public static HostObjAttributes ToRevit(this HostPartitionType hostPartitionType, Document document, Core.Revit.ConvertSettings convertSettings) { if (hostPartitionType == null) { return(null); } HostObjAttributes result = null; List <HostObjAttributes> hostObjAttributesList = convertSettings?.GetObjects <HostObjAttributes>(hostPartitionType.Guid); if (hostObjAttributesList != null) { if (hostPartitionType is WallType) { result = hostObjAttributesList.Find(x => x is Autodesk.Revit.DB.WallType); } else if (hostPartitionType is FloorType) { result = hostObjAttributesList.Find(x => x is Autodesk.Revit.DB.FloorType); } else if (hostPartitionType is RoofType) { result = hostObjAttributesList.Find(x => x is Autodesk.Revit.DB.RoofType); } } if (result != null) { return(result); } FilteredElementCollector filteredElementCollector = Query.FilteredElementCollector_New(document, hostPartitionType.GetType())?.OfClass(typeof(HostObjAttributes)); if (filteredElementCollector == null) { return(null); } string familyName_Source = null; string typeName_Source = null; if (!Core.Revit.Query.TryGetFamilyNameAndTypeName(hostPartitionType.Name, out familyName_Source, out typeName_Source)) { return(null); } foreach (HostObjAttributes hostObjAttributes in filteredElementCollector) { string fullName = Core.Revit.Query.FullName(hostObjAttributes); string familyName = null; string typeName = null; if (!Core.Revit.Query.TryGetFamilyNameAndTypeName(fullName, out familyName, out typeName)) { continue; } if (fullName != null && fullName.Equals(hostPartitionType.Name)) { result = hostObjAttributes; break; } if (!string.IsNullOrWhiteSpace(familyName) && !string.IsNullOrWhiteSpace(familyName_Source)) { continue; } if (typeName.Equals(typeName_Source)) { result = hostObjAttributes; } } if (result == null) { return(result); } List <Architectural.MaterialLayer> materialLayers = hostPartitionType.MaterialLayers; if (materialLayers != null && materialLayers.Count != 0) { CompoundStructure compoundStructure = result.GetCompoundStructure(); if (compoundStructure != null) { List <Material> materials = new FilteredElementCollector(document).OfClass(typeof(Material)).Cast <Material>().ToList(); List <CompoundStructureLayer> compoundStructureLayers = new List <CompoundStructureLayer>(); foreach (Architectural.MaterialLayer materialLayer in materialLayers) { Material material = materials.Find(x => x.Name == materialLayer.Name); if (material == null) { continue; } double width = double.NaN; #if Revit2017 || Revit2018 || Revit2019 || Revit2020 width = UnitUtils.ConvertToInternalUnits(materialLayer.Thickness, DisplayUnitType.DUT_METERS); #else width = UnitUtils.ConvertToInternalUnits(materialLayer.Thickness, UnitTypeId.Meters); #endif CompoundStructureLayer compoundStructureLayer = new CompoundStructureLayer(width, MaterialFunctionAssignment.Structure, material.Id); if (compoundStructureLayer == null) { continue; } compoundStructureLayers.Add(compoundStructureLayer); } if (compoundStructureLayers != null && compoundStructureLayers.Count != 0) { compoundStructure.SetLayers(compoundStructureLayers); result.SetCompoundStructure(compoundStructure); } } } if (convertSettings.ConvertParameters) { Core.Revit.Modify.SetValues(result, hostPartitionType); Core.Revit.Modify.SetValues(result, hostPartitionType, ActiveSetting.Setting); } convertSettings?.Add(hostPartitionType.Guid, result); return(result); }
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); } }