public void Execute(UpdaterData updateData)
 {
     try
     {
         var doc        = updateData.GetDocument();
         var deletes    = updateData.GetDeletedElementIds();
         var collection = CSAContext.GetCollection(doc);
         if (deletes.Count == 0)
         {
             return;
         }
         bool isDeleted = false;
         foreach (var deleteId in deletes)
         {
             var itemToDelete = collection.Data.FirstOrDefault(c => c.TargetId.IntegerValue == deleteId.IntegerValue);
             if (itemToDelete == null)
             {
                 itemToDelete = collection.Data.FirstOrDefault(c => c.LineId.IntegerValue == deleteId.IntegerValue);
             }
             if (itemToDelete == null)
             {
                 itemToDelete = collection.Data.FirstOrDefault(c => c.TextNoteIds.FirstOrDefault(p => p.IntegerValue == deleteId.IntegerValue) != null);
             }
             if (itemToDelete != null)
             {
                 collection.Data.Remove(itemToDelete);
                 var creater = CSAContext.Creator;
                 creater.Clear(doc, itemToDelete);
                 isDeleted = true;
             }
         }
         if (isDeleted)
         {
             CSAContext.Save(doc);
         }
     }
     catch (Exception ex)
     {
         VLLogHelper.Error(ex);
     }
 }
Ejemplo n.º 2
0
        public override void Execute()
        {
            var viewType = (CSAViewType)Enum.Parse(typeof(CSAViewType), ViewType.ToString());

            switch (viewType)
            {
            case CSAViewType.Idle:
                View = new CompoundStructureAnnotationWindow(this);
                IntPtr rvtPtr = Autodesk.Windows.ComponentManager.ApplicationWindow;
                WindowInteropHelper helper = new WindowInteropHelper(View);
                helper.Owner = rvtPtr;
                View.ShowDialog();
                break;

            case CSAViewType.Select:
                if (View.IsActive)
                {
                    View.Close();
                }
                using (PmSoft.Common.RevitClass.PickObjectsMouseHook MouseHook = new PmSoft.Common.RevitClass.PickObjectsMouseHook())
                {
                    MouseHook.InstallHook(PmSoft.Common.RevitClass.PickObjectsMouseHook.OKModeENUM.Objects);
                    try
                    {
                        Model.TargetId = UIDocument.Selection.PickObject(ObjectType.Element
                                                                         , new VLClassesFilter(false, typeof(Wall), typeof(Floor), typeof(ExtrusionRoof), typeof(FootPrintRoof))).ElementId;
                        MouseHook.UninstallHook();
                        ViewType = (int)CSAViewType.Generate;
                    }
                    catch (Exception ex)
                    {
                        MouseHook.UninstallHook();
                        ViewType = (int)CSAViewType.Idle;
                    }
                }
                break;

            case CSAViewType.Generate:
                var doc = UIDocument.Document;
                if (VLTransactionHelper.DelegateTransaction(doc, "生成结构标注", () =>
                {
                    var element = doc.GetElement(Model.TargetId);
                    var Collection = CSAContext.GetCollection(doc);
                    //避免重复生成 由于一个对象可能在不同的视图中进行标注设置 所以还是需要重复生成的
                    var existedModel = Collection.Data.FirstOrDefault(c => c.TargetId.IntegerValue == Model.TargetId.IntegerValue);
                    if (existedModel != null)
                    {
                        Collection.Data.Remove(existedModel);
                        CSAContext.Creator.Clear(doc, existedModel);
                    }
                    var fontScale = 1 / VLConstraintsForCSA.OrientFontSizeScale * Model.CurrentFontSizeScale;
                    Model.LineWidth = UnitHelper.ConvertToFoot(Model.CSALocationType.GetLineWidth() * fontScale, VLUnitType.millimeter);
                    CSAContext.Creator.Generate(doc, Model, element);
                    Collection.Data.Add(Model);
                    Collection.Save(doc);
                    return(true);
                }))
                {
                    ViewType = (int)CSAViewType.Select;
                }
                else
                {
                    ViewType = (int)CSAViewType.Idle;
                }
                break;

            case CSAViewType.Close:
                View.Close();
                break;

            case CSAViewType.Closing:
            default:
                break;
            }
        }
        public void Execute(UpdaterData updateData)
        {
            try
            {
                var document   = updateData.GetDocument();
                var edits      = updateData.GetModifiedElementIds();
                var collection = CSAContext.GetCollection(document);
                if (CSAContext.IsEditing == true)
                {
                    CSAContext.IsEditing = false;
                    return;
                }
                List <int> movedEntities = new List <int>();
                foreach (var changeId in edits)
                {
                    CSAModel model = null;
                    if (VLConstraintsForCSA.Doc == null)
                    {
                        VLConstraintsForCSA.Doc = document;
                    }

                    #region 根据Target重新生成
                    var targetMoved = collection.Data.FirstOrDefault(c => c.TargetId.IntegerValue == changeId.IntegerValue);
                    if (targetMoved != null)
                    {
                        model = targetMoved;
                        if (movedEntities.Contains(model.TargetId.IntegerValue))
                        {
                            continue;
                        }
                        var creater = CSAContext.Creator;
                        var target  = document.GetElement(model.TargetId);//标注主体失效时删除
                        if (target == null)
                        {
                            collection.Data.Remove(model);
                            continue;
                        }
                        CSAContext.Creator.Regenerate(document, model, target, new XYZ(0, 0, 0));
                        movedEntities.Add(model.TargetId.IntegerValue);
                        CSAContext.IsEditing = true;
                    }
                    #endregion

                    #region 根据Text重新生成
                    var textMoved = collection.Data.FirstOrDefault(c => c.TextNoteIds.FirstOrDefault(p => p.IntegerValue == changeId.IntegerValue) != null);
                    if (textMoved != null)
                    {
                        model = textMoved;
                        if (movedEntities.Contains(model.TargetId.IntegerValue))
                        {
                            continue;
                        }
                        var creater = CSAContext.Creator;
                        var target  = document.GetElement(model.TargetId);//标注主体失效时删除
                        if (target == null)
                        {
                            collection.Data.Remove(model);
                            continue;
                        }
                        //文本更改处理
                        var index   = model.TextNoteIds.IndexOf(changeId);
                        var newText = (document.GetElement(changeId) as TextNote).Text;
                        if (model.Texts[index] != newText)
                        {
                            CompoundStructure compoundStructure = null;
                            HostObjAttributes hoster            = null;
                            if (target is Wall)
                            {
                                hoster            = (HostObjAttributes)((target as Wall).WallType);
                                compoundStructure = hoster.GetCompoundStructure();
                            }
                            if (target is Floor)
                            {
                                hoster            = (HostObjAttributes)((target as Floor).FloorType);
                                compoundStructure = hoster.GetCompoundStructure();
                            }
                            if (target is RoofBase)//屋顶有多种类型
                            {
                                hoster            = (HostObjAttributes)((target as RoofBase).RoofType);
                                compoundStructure = hoster.GetCompoundStructure();
                            }
                            if (compoundStructure == null)
                            {
                                throw new NotImplementedException("关联更新失败,未获取有效的CompoundStructure类型");
                            }
                            var    layers  = compoundStructure.GetLayers();
                            string pattern = @"([\d+\.]+)厚(.+)[\r]?";
                            Regex  regex   = new Regex(pattern);
                            var    match   = regex.Match(newText);
                            if (!match.Success)
                            {
                                PMMessageBox.ShowError("关联更新失败,文本不符合规范");
                                return;
                            }
                            var length       = match.Groups[1].Value;
                            var lengthFoot   = UnitHelper.ConvertToFoot(Convert.ToDouble(length), VLUnitType.millimeter);
                            var materialName = match.Groups[2].Value;
                            var material     = new FilteredElementCollector(document).OfClass(typeof(Material))
                                               .FirstOrDefault(c => c.Name == materialName);
                            if (material == null)
                            {
                                PMMessageBox.ShowError("关联更新失败,未获取到对应名称的材质");
                                return;
                            }
                            //更新
                            layers[index].Width      = lengthFoot;
                            layers[index].MaterialId = material.Id;
                            compoundStructure.SetLayers(layers);
                            IDictionary <int, CompoundStructureError> report = null;
                            IDictionary <int, int> errorMap;
                            try
                            {
                                compoundStructure.IsValid(document, out report, out errorMap);
                                hoster.SetCompoundStructure(compoundStructure);
                            }
                            catch (Exception ex)
                            {
                                PMMessageBox.ShowError("材质设置失败,错误详情:" + (report != null ? string.Join(",", report.Select(c => c.Value)) : ""));
                                throw ex;
                            }

                            ////报错This operation is valid only for non-vertically compound structures
                            //layer = layers[index];
                            //layer.MaterialId = material.Id;
                            ////compoundStructure.SetLayer(index, layer);
                        }
                        else
                        {
                            var textNote = document.GetElement(changeId) as TextNote;
                            if (model.TextNoteTypeElementId.IntegerValue != textNote.TextNoteType.Id.IntegerValue)
                            {
                                model.TextNoteTypeElementId = textNote.TextNoteType.Id;
                                CSAContext.Creator.Regenerate(document, model, target, new XYZ(0, 0, 0));
                            }
                            else
                            {
                                //文本偏移处理
                                //var index = model.TextNoteIds.IndexOf(changeId);
                                //var offset = (document.GetElement(changeId) as TextNote).Coord - model.TextLocations[index];
                                //CompoundStructureAnnotationContext.Creater.Regenerate(document, model, target, offset);
                                //CSAContext.IsEditing = true;//移动会导致偏移 从而二次触发
                            }
                        }
                        movedEntities.Add(model.TargetId.IntegerValue);
                    }
                    #endregion

                    #region 根据Line重新生成
                    var lineMoved = collection.Data.FirstOrDefault(c => c.LineId.IntegerValue == changeId.IntegerValue);
                    if (lineMoved != null)
                    {
                        model = lineMoved;
                        if (movedEntities.Contains(model.TargetId.IntegerValue))
                        {
                            continue;
                        }
                        var creater = CSAContext.Creator;
                        var target  = document.GetElement(model.TargetId);
                        if (target == null)
                        {
                            collection.Data.Remove(model);
                            continue;
                        }
                        CSAContext.Creator.Regenerate(document, model, target);
                        movedEntities.Add(model.TargetId.IntegerValue);
                        CSAContext.IsEditing = true;
                    }
                    #endregion
                }
                CSAContext.Save(document);
            }
            catch (Exception ex)
            {
                VLLogHelper.Error(ex);
            }
        }