话不多说,直接上代码!

 public class WorkSetHelper
 {
        public void AddElementsToWorkSet(Document doc, List<Element> elements)
        {
            if (doc.IsWorkshared == true)
            {
                var workset = GetWorkset(doc);
                if (workset != null)
                {
                    var worksetID = workset.Id.IntegerValue;
                    using (Transaction tran = new Transaction(doc, "[ToolSet] Add Elemens To WorkSet"))
                    {
                        tran.Start();
                        foreach (var ele in elements)
                        {
                            Parameter wsparam = ele.get_Parameter(BuiltInParameter.ELEM_PARTITION_PARAM);
                            if (wsparam != null)
                            {
                                wsparam.Set(worksetID);
                            }                          
                        }
                        tran.Commit();
                    }                   
                }               
            }
        }

        public Workset GetWorkset(Document doc)
        {
            Workset newWorkset = null;
            // Worksets can only be created in a document with worksharing enabled
            if (doc.IsWorkshared)
            {
                string worksetName = "WorkSetName";
                // Workset name must not be in use by another workset
                if (WorksetTable.IsWorksetNameUnique(doc, worksetName))
                {
                    using (Transaction tran = new Transaction(doc, "[ToolSet] Create Work Set For ToolSet"))
                    {
                        tran.Start();
                        newWorkset = Workset.Create(doc, worksetName);
                        tran.Commit();
                    }
                }
                else
                {
                    IList<Workset> worksetList = new FilteredWorksetCollector(doc).OfKind(WorksetKind.UserWorkset).ToWorksets();
                    foreach (Workset workset in worksetList)
                    {
                        if (workset.Name.Contains(worksetName))
                        {
                            return workset;
                        }
                    }
                }
            }
            return newWorkset;
        }
 }

 

结尾:

        进入设计院两周,发现市面上的Revit插件与设计院的需求差距还是很大的,设计院对视图的显示方面的要求较高。一个人在中心从事Revit插件开发压力山大啊,告诉自己慢慢来,能赢!

posted on 2018-05-17 16:30  airforce094  阅读(921)  评论(0编辑  收藏  举报