public override void LoadConfig(Document doc) { IsSuccess = DelegateHelper.DelegateTransaction(doc, "标注族加载", () => { return(PAContext.LoadFamilySymbols(doc)); });// && PipeAnnotationContext.LoadParameterOfSymbols(doc); if (!IsSuccess) { TaskDialog.Show("错误", "加载必要的族时失败"); return; } //PipeAnnotationContext.LoadParameterOfSymbols(doc); DelegateHelper.DelegateTransaction(doc, "加载配置记忆", () => { SettingForSingle = new SinglePipeAnnotationSettings(this); SettingForMultiple = new MultiPipeAnnotationSettings(this); SettingForCommon = new CommonAnnotationSettings(); var data = PAContext.GetSetting(doc); var values = data.Split(Splitter.ToCharArray().First()); if (values.Count() >= 7) { SettingForSingle.LengthFromLine_Milimeter = Convert.ToInt32(values[0]); SettingForSingle.Location = (SinglePipeTagLocation)Enum.Parse(typeof(SinglePipeTagLocation), values[1]); SettingForSingle.NeedLeader = Convert.ToBoolean(values[2]); SettingForMultiple.LengthBetweenPipe_Milimeter = Convert.ToInt32(values[3]); SettingForMultiple.Location = (MultiPipeTagLocation)Enum.Parse(typeof(MultiPipeTagLocation), values[4]); SettingForCommon.IncludeLinkPipe = Convert.ToBoolean(values[5]); SettingForCommon.AutoPreventCollision = Convert.ToBoolean(values[6]); //0728长度过滤 if (values.Count() > 7) { SettingForCommon.MinLength_Milimeter = Convert.ToInt32(values[7]); SettingForCommon.FilterVertical = Convert.ToBoolean(values[8]); } } return(true); }); IsSuccess = true; }
/// <summary> /// 生成标注 /// </summary> public AnnotationBuildResult GenerateMultipleTagSymbol(Document document, IEnumerable <ElementId> selectedIds, MultiPipeAnnotationSettings setting, bool generateSingleOne) { if (selectedIds.Count() > AnnotationConstaints.PipeCountMax) { throw new NotImplementedException("暂不支持8根以上及管道的多管直径标注生成"); } Document = document; Collection = PAContext.GetCollection(Document); PipeAnnotationEntity entity = new PipeAnnotationEntity(); entity.LocationType = setting.Location; View view = Document.ActiveView; AnnotationBuildResult result = GenerateMultipleTagSymbol(document, selectedIds, entity, setting); if (generateSingleOne && result == AnnotationBuildResult.Success) { Collection.Add(entity); Collection.Save(Document); } else if (result == AnnotationBuildResult.Success) { Collection.Add(entity); } return(result); }
/// <summary> /// 生成标注 /// </summary> /// <param name="selectedIds"></param> /// <param name="entity"></param> /// <param name="view"></param> /// <returns></returns> AnnotationBuildResult GenerateMultipleTagSymbol(Document document, IEnumerable <ElementId> selectedIds, PipeAnnotationEntity entity, MultiPipeAnnotationSettings setting) { View view = document.ActiveView; XYZ startPoint = null; FamilyInstance line = null; List <PipeAndNodePoint> pipeAndNodePoints = new List <PipeAndNodePoint>(); var tags = new List <IndependentTag>(); //管道 获取 foreach (var selectedId in selectedIds) { pipeAndNodePoints.Add(new PipeAndNodePoint(Document.GetElement(selectedId) as Pipe)); } //平行,垂直 向量 XYZ parallelVector = null; XYZ verticalVector = null; parallelVector = ((pipeAndNodePoints.First().Pipe.Location as LocationCurve).Curve as Line).Direction; verticalVector = new XYZ(parallelVector.Y, -parallelVector.X, 0); parallelVector = LocationHelper.GetVectorByQuadrant(parallelVector, QuadrantType.OneAndFour); verticalVector = LocationHelper.GetVectorByQuadrant(verticalVector, QuadrantType.OneAndTwo); if (parallelVector.Y == 1) { verticalVector = -verticalVector; } //平行检测 if (!CheckParallel(pipeAndNodePoints.Select(c => c.Pipe), verticalVector)) { return(AnnotationBuildResult.NotParallel); } XYZ rightOfLefts; //左侧点的右边界 XYZ leftOfRights; //右侧点的左边界 //节点计算 if (!GetNodePoints(pipeAndNodePoints, out rightOfLefts, out leftOfRights)) { return(AnnotationBuildResult.NoOverlap); } pipeAndNodePoints = pipeAndNodePoints.OrderByDescending(c => c.NodePoint.Y).ToList(); var pipes = pipeAndNodePoints.Select(c => c.Pipe).ToList(); var orderedNodePoints = pipeAndNodePoints.Select(c => c.NodePoint).ToList(); //起始点 startPoint = orderedNodePoints.First(); //线 创建 var multipleTagSymbol = PAContext.GetMultipleTagSymbol(document); if (!multipleTagSymbol.IsActive) { multipleTagSymbol.Activate(); } line = Document.Create.NewFamilyInstance(startPoint, multipleTagSymbol, view); //线 旋转处理 LocationPoint locationPoint = line.Location as LocationPoint; if (locationPoint != null) { locationPoint.RotateByXY(startPoint, verticalVector); } //线 参数设置 UpdateLineParameters(orderedNodePoints, line, verticalVector); //标注 创建 var textSize = PAContext.TextSize; var widthScale = PAContext.WidthScale; //碰撞检测区域点 XYZ avoidP1_Line1 = orderedNodePoints.Last(); XYZ avoidP2_Line2 = null; XYZ avoidP3_Annotation1 = null; switch (entity.LocationType) { case MultiPipeTagLocation.OnLineEdge: //添加对应的单管直径标注 line.GetParameters(TagProperty.线宽度.ToString()).First().Set(UnitHelper.ConvertToFoot(200, AnnotationConstaints.UnitType)); var height = Convert.ToDouble(line.GetParameters(TagProperty.线高度1.ToString()).First().AsValueString()) + (orderedNodePoints.Count() - 1) * AnnotationConstaints.TextHeight; avoidP2_Line2 = avoidP1_Line1 + UnitHelper.ConvertToFoot(height, AnnotationConstaints.UnitType) * verticalVector; var skewLength = AnnotationConstaints.SkewLengthForOffLine; for (int i = 0; i < pipes.Count(); i++) { var subTag = Document.Create.NewTag(view, pipes[i], false, TagMode.TM_ADDBY_CATEGORY, TagOrientation.Horizontal, startPoint); var text = subTag.TagText; var textLength = System.Windows.Forms.TextRenderer.MeasureText(text, AnnotationConstaints.Font).Width; var actualLength = textLength / (textSize * widthScale); subTag.TagHeadPosition = startPoint + skewLength * parallelVector + UnitHelper.ConvertToInch(actualLength, VLUnitType.millimeter) * parallelVector + UnitHelper.ConvertToFoot(height - i * AnnotationConstaints.TextHeight, AnnotationConstaints.UnitType) * verticalVector; tags.Add(subTag); if (i == 0) { avoidP3_Annotation1 = subTag.TagHeadPosition + skewLength * parallelVector + UnitHelper.ConvertToInch(actualLength, VLUnitType.millimeter) * parallelVector + UnitHelper.ConvertToFoot(0.5 * AnnotationConstaints.TextHeight, AnnotationConstaints.UnitType) * verticalVector; } } break; case MultiPipeTagLocation.OnLine: //添加对应的单管直径标注 line.GetParameters(TagProperty.线宽度.ToString()).First().Set(UnitHelper.ConvertToFoot(800, AnnotationConstaints.UnitType)); height = Convert.ToDouble(line.GetParameters(TagProperty.线高度1.ToString()).First().AsValueString()) + (orderedNodePoints.Count() - 1) * AnnotationConstaints.TextHeight; avoidP2_Line2 = avoidP1_Line1 + UnitHelper.ConvertToFoot(height, AnnotationConstaints.UnitType) * verticalVector; skewLength = AnnotationConstaints.SkewLengthForOnLine; for (int i = 0; i < pipes.Count(); i++) { var subTag = Document.Create.NewTag(view, pipes[i], false, TagMode.TM_ADDBY_CATEGORY, TagOrientation.Horizontal, startPoint); var text = subTag.TagText; var textLength = System.Windows.Forms.TextRenderer.MeasureText(text, AnnotationConstaints.Font).Width; var actualLength = textLength / (textSize * widthScale); subTag.TagHeadPosition = startPoint + skewLength * parallelVector + UnitHelper.ConvertToInch(actualLength, VLUnitType.millimeter) * parallelVector + UnitHelper.ConvertToFoot(height - i * AnnotationConstaints.TextHeight + 0.5 * AnnotationConstaints.TextHeight, AnnotationConstaints.UnitType) * verticalVector; tags.Add(subTag); if (i == 0) { avoidP3_Annotation1 = subTag.TagHeadPosition + skewLength * parallelVector + UnitHelper.ConvertToInch(actualLength, VLUnitType.millimeter) * parallelVector + UnitHelper.ConvertToFoot(AnnotationConstaints.TextHeight, AnnotationConstaints.UnitType) * verticalVector; } } break; default: return(AnnotationBuildResult.NoLocationType); } entity.ViewId = view.Id.IntegerValue; entity.LineId = line.Id.IntegerValue; foreach (var pipe in pipes) { entity.PipeIds.Add(pipe.Id.IntegerValue); } foreach (var tag in tags) { entity.TagIds.Add(tag.Id.IntegerValue); } entity.StartPoint = startPoint; //碰撞检测 VLTriangle triangle = new VLTriangle(avoidP1_Line1, avoidP2_Line2, avoidP3_Annotation1); List <Line> lines = triangle.GetLines(); AvoidData data = new AvoidData(document, selectedIds, entity, lines, triangle, multipleTagSymbol, parallelVector, rightOfLefts.GetLength(), leftOfRights.GetLength()); AvoidStrategy strategty = AvoidStrategy.MoveLeft; var strategyEntity = AvoidStrategyFactory.GetAvoidStrategyEntity(strategty); strategyEntity.Data = data; while (strategyEntity.CheckCollision(strategyEntity.Data)) { if (strategyEntity.TryAvoid()) { strategyEntity.Apply(data); break; } else { strategyEntity = strategyEntity.GetNextStratetyEntity(); } } return(AnnotationBuildResult.Success); #region old //if (CheckCollision(document, view, selectedIds, line, lines, triangle, multipleTagSymbol)) //{ // int offsetLength = 10; // XYZ offset = null; // if (rightOfLefts.GetLength() > offsetLength) // offset = parallelVector * offsetLength; // else if (leftOfRights.GetLength() > offsetLength) // offset = -parallelVector * offsetLength; // else // return AnnotationBuildResult.Success; // avoidP1_Line1 += offset; // avoidP2_Line2 += offset; // avoidP3_Annotation1 += offset; // lines = GetLines(avoidP1_Line1, avoidP2_Line2, avoidP3_Annotation1); // triangle = new Triangle(avoidP1_Line1, avoidP2_Line2, avoidP3_Annotation1); // if (!CheckCollision(document, view, selectedIds, line, lines, triangle, multipleTagSymbol)) // { // //TODO 偏移处理 // Autodesk.Revit.DB.ElementTransformUtils.MoveElement(document, new ElementId(entity.LineId), offset); // foreach (var tagId in entity.TagIds) // { // Autodesk.Revit.DB.ElementTransformUtils.MoveElement(document, new ElementId(tagId), offset); // } // } //} //return AnnotationBuildResult.Success; #endregion }