예제 #1
0
        private void AddSharedParameters(Application app, Document doc)
        {
            //Save the previous shared param file path
            string previousSharedParam = app.SharedParametersFilename;

            //Extract shared param to a txt file
            string tempPath = System.IO.Path.GetTempPath();
            string SPPath   = Path.Combine(tempPath, "bimsyncSharedParameter.txt");

            if (!File.Exists(SPPath))
            {
                //extract the familly
                List <string> files = new List <string>();
                files.Add("bimsyncSharedParameter.txt");
                Services.ExtractEmbeddedResource(tempPath, "bimsync.Resources", files);
            }

            //set the shared param file
            app.SharedParametersFilename = SPPath;

            //Define a category set containing the project properties
            CategorySet myCategorySet   = app.Create.NewCategorySet();
            Categories  categories      = doc.Settings.Categories;
            Category    projectCategory = categories.get_Item(BuiltInCategory.OST_ProjectInformation);

            myCategorySet.Insert(projectCategory);

            //Retrive shared parameters
            DefinitionFile myDefinitionFile = app.OpenSharedParameterFile();

            DefinitionGroup definitionGroup = myDefinitionFile.Groups.get_Item("bimsync");

            foreach (Definition paramDef in definitionGroup.Definitions)
            {
                // Get the BingdingMap of current document.
                BindingMap bindingMap = doc.ParameterBindings;

                //the parameter does not exist
                if (!bindingMap.Contains(paramDef))
                {
                    //Create an instance of InstanceBinding
                    InstanceBinding instanceBinding = app.Create.NewInstanceBinding(myCategorySet);

                    bindingMap.Insert(paramDef, instanceBinding, BuiltInParameterGroup.PG_IDENTITY_DATA);
                }
                //the parameter is not added to the correct categories
                else if (bindingMap.Contains(paramDef))
                {
                    InstanceBinding currentBinding = bindingMap.get_Item(paramDef) as InstanceBinding;
                    currentBinding.Categories = myCategorySet;
                    bindingMap.ReInsert(paramDef, currentBinding, BuiltInParameterGroup.PG_IDENTITY_DATA);
                }
            }

            //Reset to the previous shared parameters text file
            app.SharedParametersFilename = previousSharedParam;
            File.Delete(SPPath);
        }
예제 #2
0
        //Method deletes parameters
        public void RemoveSharedParameterBinding(Application app, string name, ParameterType type)
        {
            BindingMap map = (new UIApplication(app)).ActiveUIDocument.Document.ParameterBindings;
            DefinitionBindingMapIterator it = map.ForwardIterator();

            it.Reset();

            Definition def = null;

            while (it.MoveNext())
            {
                if (it.Key != null && it.Key.Name == name && type == it.Key.ParameterType)
                {
                    def = it.Key;
                    break;
                }
            }

            if (def == null)
            {
                sbFeedback.Append("Parameter " + name + " does not exist.\n");
            }
            else
            {
                map.Remove(def);
                if (map.Contains(def))
                {
                    sbFeedback.Append("Failed to delete parameter " + name + " for some reason.\n");
                }
                else
                {
                    sbFeedback.Append("Parameter " + name + " deleted.\n");
                }
            }
        }
예제 #3
0
        internal void CreateBinding(string domain, string tempFile, Application app, Document doc, CategorySet catSet, StringBuilder sbFeedback)
        {
            //Parameter query

            var query = from pdef p in new pl().PL where string.Equals(p.Domain, domain) select p;

            //Create parameter bindings
            try
            {
                foreach (pdef parameter in query.ToList())
                {
                    using (File.Create(tempFile)) { }
                    app.SharedParametersFilename = tempFile;
                    ExternalDefinitionCreationOptions options = new ExternalDefinitionCreationOptions(parameter.Name, parameter.Type)
                    {
                        GUID = parameter.Guid
                    };
                    ExternalDefinition def = app.OpenSharedParameterFile().Groups.Create("TemporaryDefinitionGroup").Definitions.Create(options) as ExternalDefinition;

                    BindingMap map     = doc.ParameterBindings;
                    Binding    binding = app.Create.NewInstanceBinding(catSet);

                    if (map.Contains(def))
                    {
                        sbFeedback.Append("Parameter " + parameter.Name + " already exists.\n");
                    }
                    else
                    {
                        map.Insert(def, binding, pd.PCF_BUILTIN_GROUP_NAME);
                        if (map.Contains(def))
                        {
                            sbFeedback.Append("Parameter " + parameter.Name + " added to project.\n");
                        }
                        else
                        {
                            sbFeedback.Append("Creation of parameter " + parameter.Name + " failed for some reason.\n");
                        }
                    }
                    File.Delete(tempFile);
                }
            }

            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
예제 #4
0
        internal Result CreatePipelineBindings(UIApplication uiApp, ref string msg)
        {
            // UIApplication uiApp = commandData.Application;
            Document    doc = uiApp.ActiveUIDocument.Document;
            Application app = doc.Application;

            Autodesk.Revit.Creation.Application ca = app.Create;

            Category pipelineCat = doc.Settings.Categories.get_Item(BuiltInCategory.OST_PipingSystem);

            CategorySet catSet = ca.NewCategorySet();

            catSet.Insert(pipelineCat);

            string ExecutingAssemblyPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string oriFile  = app.SharedParametersFilename;
            string tempFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()) + ".txt";

            using (File.Create(tempFile)) { }
            uiApp.Application.SharedParametersFilename = tempFile;
            app.SharedParametersFilename = tempFile;
            DefinitionFile file   = app.OpenSharedParameterFile();
            var            groups = file.Groups;
            int            count  = 0;

            StringBuilder sbFeedback = new StringBuilder();

            //Parameter query
            var query = from p in new plst().LPAll
                        where (p.Domain == "PIPL" || p.Name == "PCF_PIPL_EXCL") && p.ExportingTo != "LDT"
                        select p;

            //Create parameter bindings
            Transaction trans = new Transaction(doc, "Bind PCF parameters");

            trans.Start();

            foreach (pdef parameter in query.ToList())
            {
                count++;

                ExternalDefinitionCreationOptions options = new ExternalDefinitionCreationOptions(parameter.Name, parameter.Type)
                {
                    GUID = parameter.Guid
                };

                string             tempName = "TemporaryDefinitionGroup" + count;
                var                tempGr   = groups.Create(tempName);
                var                tempDefs = tempGr.Definitions;
                ExternalDefinition def      = tempDefs.Create(options) as ExternalDefinition;

                BindingMap map     = doc.ParameterBindings;
                Binding    binding = app.Create.NewTypeBinding(catSet);

                if (map.Contains(def))
                {
                    sbFeedback.Append("Parameter " + parameter.Name + " already exists.\n");
                }
                else
                {
                    map.Insert(def, binding, InputVars.PCF_BUILTIN_GROUP_NAME);
                    if (map.Contains(def))
                    {
                        sbFeedback.Append("Parameter " + parameter.Name + " added to project.\n");
                    }
                    else
                    {
                        sbFeedback.Append("Creation of parameter " + parameter.Name + " failed for some reason.\n");
                    }
                }
            }
            trans.Commit();
            BuildingCoderUtilities.InfoMsg(sbFeedback.ToString());
            File.Delete(tempFile);

            app.SharedParametersFilename = oriFile;

            return(Result.Succeeded);
        }
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument    uidoc = uiapp.ActiveUIDocument;

            Autodesk.Revit.ApplicationServices.Application app = uiapp.Application;
            Document doc = uidoc.Document;

            ICollection <ElementId> viewIdCollection = new List <ElementId>();

            #region
            void SetCollectionToRemove()
            {
                try
                {
                    #region
                    // To add all views to collection
                    FilteredElementCollector viewCollector = new FilteredElementCollector(doc);
                    viewCollector.OfClass(typeof(Autodesk.Revit.DB.View));

                    string activeViewName = uidoc.ActiveView.Name;

                    foreach (Autodesk.Revit.DB.View v in viewCollector)
                    {
                        if (v.ViewType.ToString() != "ProjectBrowser" && v.ViewType.ToString() != "SystemBrowser" && v.Name != activeViewName)
                        {
                            viewIdCollection.Add(v.Id);
                        }
                    }
                    #endregion

                    #region
                    // To add all project parameters to collection
                    BindingMap bindingMap = doc.ParameterBindings;

                    FilteredElementCollector parameterElementCollector = new FilteredElementCollector(doc);
                    parameterElementCollector.OfClass(typeof(ParameterElement));

                    foreach (ParameterElement parameterElement in parameterElementCollector)
                    {
                        if (bindingMap.Contains(parameterElement.GetDefinition()))
                        {
                            viewIdCollection.Add(parameterElement.Id);
                        }
                    }
                    #endregion

                    PurgeUnused();
                    TaskDialog.Show("Revit Handy Tools - Model Transmit", "The project will be prepared for model transmit");
                }
                catch (Exception e)
                {
                    TaskDialog.Show("Error", e.ToString());
                }
            }

            #endregion

            void PurgeUnused()
            {
                // To purge a project
                RevitCommandId commandId = RevitCommandId.LookupCommandId("ID_PURGE_UNUSED");

                uiapp.PostCommand(commandId);
            }

            CustomForms.WarningForm warningTransmit = new CustomForms.WarningForm();
            warningTransmit.WarningLabel = String.Format("{0}{1}", "This will remove critical settings of the project.\n",
                                                         "Please make sure you run this extension in a coordiantion model.");
            warningTransmit.ShowDialog();

            if (warningTransmit.DialogResult == DialogResult.Yes)
            {
                SetCollectionToRemove();
            }
            else
            {
                return(Result.Succeeded);
            }

            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("Cleaning up a project for model transmit");

                doc.Delete(viewIdCollection);

                tx.Commit();
            }

            return(Result.Succeeded);
        }
예제 #6
0
        internal Result CreatePipelineBindings(UIApplication uiApp, ref string msg)
        {
            // UIApplication uiApp = commandData.Application;
            Document    doc = uiApp.ActiveUIDocument.Document;
            Application app = doc.Application;

            Autodesk.Revit.Creation.Application ca = app.Create;

            Category pipelineCat = doc.Settings.Categories.get_Item(BuiltInCategory.OST_PipingSystem);

            CategorySet catSet = ca.NewCategorySet();

            catSet.Insert(pipelineCat);

            string ExecutingAssemblyPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string oriFile  = app.SharedParametersFilename;
            string tempFile = ExecutingAssemblyPath + "Temp.txt";

            StringBuilder sbFeedback = new StringBuilder();

            //Parameter query
            var query = from p in new plst().LPAll
                        where p.Domain == "PIPL" ||
                        p.Name == "CII_PIPL_EXCL"
                        select p;

            //Create parameter bindings
            try
            {
                Transaction trans = new Transaction(doc, "Bind PCF parameters");
                trans.Start();
                foreach (pdef parameter in query.ToList())
                {
                    using (File.Create(tempFile)) { }
                    app.SharedParametersFilename = tempFile;
                    ExternalDefinitionCreationOptions options = new ExternalDefinitionCreationOptions(parameter.Name, parameter.Type)
                    {
                        GUID = parameter.Guid
                    };
                    ExternalDefinition def = app.OpenSharedParameterFile().Groups.Create("TemporaryDefinitionGroup").Definitions.Create(options) as ExternalDefinition;

                    BindingMap map     = doc.ParameterBindings;
                    Binding    binding = app.Create.NewTypeBinding(catSet);

                    if (map.Contains(def))
                    {
                        sbFeedback.Append("Parameter " + parameter.Name + " already exists.\n");
                    }
                    else
                    {
                        map.Insert(def, binding, InputVars.PCF_BUILTIN_GROUP_NAME);
                        if (map.Contains(def))
                        {
                            sbFeedback.Append("Parameter " + parameter.Name + " added to project.\n");
                        }
                        else
                        {
                            sbFeedback.Append("Creation of parameter " + parameter.Name + " failed for some reason.\n");
                        }
                    }
                    File.Delete(tempFile);
                }
                trans.Commit();
                BuildingCoderUtilities.InfoMsg(sbFeedback.ToString());
            }

            catch (Autodesk.Revit.Exceptions.OperationCanceledException) { return(Result.Cancelled); }

            catch (Exception ex)
            {
                msg = ex.Message;
                return(Result.Failed);
            }

            app.SharedParametersFilename = oriFile;

            return(Result.Succeeded);
        }
예제 #7
0
        private void AddSharedParameters(Application app, Document doc, CategorySet myCategorySet)
        {
            //Save the previous shared param file path
            string previousSharedParam = app.SharedParametersFilename;

            //Extract shared param to a txt file
            string tempPath = System.IO.Path.GetTempPath();
            string SPPath   = Path.Combine(tempPath, "FileProperties.txt");

            if (!File.Exists(SPPath))
            {
                //extract the familly
                List <string> files = new List <string>();
                files.Add("FileProperties.txt");
                Tools.ExtractEmbeddedResource(tempPath, "TimeStamp.Resources", files);
            }

            //set the shared param file
            app.SharedParametersFilename = SPPath;

            //Retrive shared parameters
            DefinitionFile myDefinitionFile = app.OpenSharedParameterFile();

            DefinitionGroup definitionGroup = myDefinitionFile.Groups.get_Item("FileProperties");

            foreach (Definition paramDef in definitionGroup.Definitions)
            {
                // Get the BingdingMap of current document.
                BindingMap bindingMap = doc.ParameterBindings;

                //the parameter does not exist
                if (!bindingMap.Contains(paramDef))
                {
                    //Create an instance of InstanceBinding
                    InstanceBinding instanceBinding = app.Create.NewInstanceBinding(myCategorySet);

                    bindingMap.Insert(paramDef, instanceBinding, BuiltInParameterGroup.PG_IDENTITY_DATA);
                }
                //the parameter is not added to the correct categories
                else if (bindingMap.Contains(paramDef))
                {
                    InstanceBinding currentBinding = bindingMap.get_Item(paramDef) as InstanceBinding;
                    currentBinding.Categories = myCategorySet;
                    bindingMap.ReInsert(paramDef, currentBinding, BuiltInParameterGroup.PG_IDENTITY_DATA);
                }
            }

            //SetAllowVaryBetweenGroups for these parameters
            string[] parameterNames = { "BIM42_Date", "BIM42_Version", "BIM42_File", "BIM42_Discipline" };

            DefinitionBindingMapIterator definitionBindingMapIterator = doc.ParameterBindings.ForwardIterator();

            definitionBindingMapIterator.Reset();

            while (definitionBindingMapIterator.MoveNext())
            {
                InternalDefinition paramDef = definitionBindingMapIterator.Key as InternalDefinition;
                if (paramDef != null)
                {
                    if (parameterNames.Contains(paramDef.Name))
                    {
                        paramDef.SetAllowVaryBetweenGroups(doc, true);
                    }
                }
            }

            //Reset to the previous shared parameters text file
            app.SharedParametersFilename = previousSharedParam;
            File.Delete(SPPath);
        }
예제 #8
0
        public Result Generate(ExternalCommandData commandData, ref string message)
        {
            Document document = commandData.Application.ActiveUIDocument.Document;

            System.Text.StringBuilder log = new System.Text.StringBuilder();

            Transaction trans = new Transaction(document, "Generate PCF parameters binding");

            trans.Start();
            try
            {
                DefinitionFile sharedParamFile = document.Application.OpenSharedParameterFile();
                if (sharedParamFile == null)
                {
                    string ExecutingAssemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                    string OriginalFile = document.Application.SharedParametersFilename;
                    string CustomFile   = ExecutingAssemblyDirectory + "\\PCFSharedParameters.txt";
                    using (File.Create(CustomFile)) { }
                    document.Application.SharedParametersFilename = CustomFile;
                    sharedParamFile = document.Application.OpenSharedParameterFile();

                    if (sharedParamFile == null)
                    {
                        throw new Exception("Fail: Create Shared Paramters for PCF Exporting.");
                    }
                }

                string groupname = "PCF_SharedParameter";

                DefinitionGroup grp = sharedParamFile.Groups.FirstOrDefault(g => g.Name == groupname);
                if (grp == null)
                {
                    grp = sharedParamFile.Groups.Create(groupname);
                }

                CategorySet categories = document.Application.Create.NewCategorySet();
                categories.Insert(document.Settings.Categories.get_Item(BuiltInCategory.OST_PipingSystem));
                categories.Insert(document.Settings.Categories.get_Item(BuiltInCategory.OST_PipeCurves));
                categories.Insert(document.Settings.Categories.get_Item(BuiltInCategory.OST_PipeFitting));
                categories.Insert(document.Settings.Categories.get_Item(BuiltInCategory.OST_PipeAccessory));

                IList <Definition> defs = new List <Definition>();

                foreach (RevitParam.ParameterDefinition pd in RevitParam.ParamterList.Values)
                {
                    if (!this.isParamExist(grp.Definitions, pd))
                    {
                        ExternalDefinitionCreationOptions edco = new ExternalDefinitionCreationOptions(pd.Name, pd.Type);
                        edco.GUID = pd.Id;
                        defs.Add(grp.Definitions.Create(edco));
                    }
                    else
                    {
                        defs.Add(grp.Definitions.get_Item(pd.Name));
                    }
                }

                BindingMap bindingMap             = document.ParameterBindings;
                Autodesk.Revit.DB.Binding binding = document.Application.Create.NewInstanceBinding(categories);

                foreach (Definition def in defs)
                {
                    if (bindingMap.Contains(def))
                    {
                        log.Append("Parameter " + def.Name + " already exists.\n");
                    }
                    else
                    {
                        bindingMap.Insert(def, binding, BuiltInParameterGroup.PG_ANALYTICAL_MODEL);
                        if (bindingMap.Contains(def))
                        {
                            log.Append("Parameter " + def.Name + " added to project.\n");
                        }
                        else
                        {
                            log.Append("Creation of parameter " + def.Name + " failed for some reason.\n");
                        }
                    }
                }

                trans.Commit();
            }
            catch (Autodesk.Revit.Exceptions.OperationCanceledException)
            {
                return(Result.Cancelled);
            }
            catch (Exception ex)
            {
                trans.RollBack();
                message = ex.Message;
                return(Result.Failed);
            }

            MessageBox.Show(log.ToString());

            return(Result.Succeeded);
        }
예제 #9
0
        /// <summary>
        /// 将外部共享参数创建为项目参数,并将其绑定到指定的类别
        /// </summary>
        /// <param name="transDoc"> bindingMap.Insert 与 bindingMap.ReInsert 方法必须在事务内才能执行</param>
        /// <param name="doc"></param>
        /// <param name="facenCategoryId"> 面层对象所属的类别 </param>
        public void BindParametersToCategory(Transaction transDoc, Document doc, ElementId facenCategoryId)
        {
            // 打开共享文件
            Autodesk.Revit.ApplicationServices.Application app = doc.Application;
            string OriginalSharedFileName = app.SharedParametersFilename;    // Revit程序中,原来的共享文件路径

            app.SharedParametersFilename = ProjectPath.SharedParameters;     // 设置Revit的共享参数文件

            DefinitionFile myDefinitionFile = app.OpenSharedParameterFile(); // 如果没有找到对应的文件,则打开时不会报错,而是直接返回Nothing
            // app.SharedParametersFilename = OriginalSharedFileName; // 将Revit程序中的共享文件路径还原,以隐藏插件程序中的共享参数文件。

            // create a new group in the shared parameters file
            DefinitionGroup myGroup = myDefinitionFile.Groups.get_Item(FaceWallParameters.sp_Group_Face);

            // 提取此共享参数组中的每一个参数,并赋值给指定的类别
            ExternalDefinition exdef_FaceIdTag = (ExternalDefinition)myGroup.Definitions.get_Item(FaceWallParameters.sp_FaceIdTag);
            ExternalDefinition exdef_FaceType  = (ExternalDefinition)myGroup.Definitions.get_Item(FaceWallParameters.sp_FaceType);
            ExternalDefinition exdef_Volumn    = (ExternalDefinition)myGroup.Definitions.get_Item(FaceWallParameters.sp_Volumn);
            ExternalDefinition exdef_Area      = (ExternalDefinition)myGroup.Definitions.get_Item(FaceWallParameters.sp_Area);

            //
            BindingMap bindingMap = doc.ParameterBindings;
            Category   myCategory = Category.GetCategory(doc, facenCategoryId);

            // ---------------------------------------------------------------------------------------------------------
            // 判断指定的类别中是否绑定有此 "面层标识 FaceIdTag" 参数
            bool faceParaHasInsertedIntoThatCategory = false;
            bool parameterHasBeenAddedToProject      = false;

            if (bindingMap.Contains(exdef_FaceIdTag))
            {
                Autodesk.Revit.DB.Binding parameterBinding = doc.ParameterBindings.get_Item(exdef_FaceIdTag);
                parameterHasBeenAddedToProject = true;

                if (parameterBinding is InstanceBinding)
                {
                    // 外部共享参数 exdef_FaceIdTag 在此文档中 绑定到了哪些类别
                    CategorySet bindedCategories = ((InstanceBinding)parameterBinding).Categories;
                    if (bindedCategories.Contains(myCategory))
                    {
                        faceParaHasInsertedIntoThatCategory = true;
                    }
                }
            }

            // ---------------------------------------------------------------------------------------------------------
            // 如果此参数"面层标识 FaceIdTag"还没有被添加到项目参数中,则先将用 Insert 其进行添加
            if (!parameterHasBeenAddedToProject)
            {
                //  在“项目参数”中添加此参数,并为其指定一个绑定的类别。
                CategorySet myCategories = app.Create.NewCategorySet();
                myCategories.Insert(myCategory);

                //Create an instance of InstanceBinding
                InstanceBinding instanceBinding = app.Create.NewInstanceBinding(myCategories);
                // Get the BingdingMap of current document.

                // Bind the definitions to the document
                bindingMap.Insert(exdef_FaceIdTag, instanceBinding, BuiltInParameterGroup.PG_IDENTITY_DATA);  //  此 Insert 操作必须在事务开启后才能执行
                bindingMap.Insert(exdef_FaceType, instanceBinding, BuiltInParameterGroup.PG_IDENTITY_DATA);
                bindingMap.Insert(exdef_Volumn, instanceBinding, BuiltInParameterGroup.PG_IDENTITY_DATA);
                bindingMap.Insert(exdef_Area, instanceBinding, BuiltInParameterGroup.PG_IDENTITY_DATA);
            }

            // ---------------------------------------------------------------------------------------------------------
            // 如果"面层标识 FaceIdTag" 参数已经绑定到了指定的类别上,就认为其他三个参数也绑定上来了,此时就不需要再绑定了;
            // 如果没有绑定,则要将这四个参数一次性全部绑定到此指定的类别上
            if (parameterHasBeenAddedToProject && !faceParaHasInsertedIntoThatCategory)
            {
                Autodesk.Revit.DB.Binding parameterBinding = doc.ParameterBindings.get_Item(exdef_FaceIdTag);
                ((InstanceBinding)parameterBinding).Categories.Insert(myCategory);  // 将新的类别添加到 binding 的类别集合中

                // 重新进行绑定
                bindingMap.ReInsert(exdef_FaceIdTag, parameterBinding, BuiltInParameterGroup.PG_IDENTITY_DATA);
                bindingMap.ReInsert(exdef_FaceType, parameterBinding, BuiltInParameterGroup.PG_IDENTITY_DATA);
                bindingMap.ReInsert(exdef_Volumn, parameterBinding, BuiltInParameterGroup.PG_IDENTITY_DATA);
                bindingMap.ReInsert(exdef_Area, parameterBinding, BuiltInParameterGroup.PG_IDENTITY_DATA);
            }
        }