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);
     }
 }
示例#2
0
 /// <summary>
 /// 保存
 /// </summary>
 /// <param name="doc"></param>
 public void Save(Document doc)
 {
     CSAContext.Save(doc);
 }
        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);
            }
        }