public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Application app = uiapp.Application; Document doc = uidoc.Document; Selection SelectedObjs = uidoc.Selection; ICollection <ElementId> ids = uidoc.Selection.GetElementIds(); ElementId defTextType = doc.GetDefaultElementTypeId(ElementTypeGroup.TextNoteType); List <double> cDiams = new List <double>(); Line LineDir = null; bool updateRack = false; List <string> Conduits = new List <string>(); TextNote toUpdate = null; RackDim.GetMenuValues(uiapp); Store.mod_split = 30; foreach (ElementId eid in ids) { Element elem = doc.GetElement(eid); if (elem.GetType() == typeof(TextNote)) { updateRack = true; toUpdate = elem as TextNote; } else { GeometryElement geom = elem.get_Geometry(Store.Dimop(doc.ActiveView)); cDiams.Add(elem.get_Parameter(BuiltInParameter.RBS_CONDUIT_DIAMETER_PARAM).AsDouble()); LineDir = RackDim.GetLineOfGeom(geom); } } if (toUpdate != null) { ids.Remove(toUpdate.Id); } double dMax = cDiams.Max(); foreach (string s in Conduits) { TaskDialog.Show("asd", s); } using (Transaction tx = new Transaction(doc)) { tx.Start("Create Text"); List <ElementId> Rows = new List <ElementId>(); List <List <ElementId> > allRows = SeparateRows(ids, doc, dMax); string diameters = ""; Double Distance = 0; List <double> leftXs = new List <double>(); List <ElementId> lefts = new List <ElementId>(); List <double> rightXs = new List <double>(); List <ElementId> rights = new List <ElementId>(); foreach (int i in SortRows(allRows, doc)) { string prev = ""; int typcount = 1; string diam; if (i == 0 && allRows.Count > 1) { diameters += "ABOVE: "; } else if (i == allRows.Count - 1 && allRows.Count != 1) { diameters += '\n' + "BELOW: "; } else if (allRows.Count > 1) { diameters += '\n' + "MIDDLE: "; } List <ElementId> SortedRows = SortX(allRows[i], doc); leftXs.Add(RackDim.GetRotatedToVert(SortedRows[0], doc).GetEndPoint(0).X); lefts.Add(SortedRows[0]); rightXs.Add(RackDim.GetRotatedToVert(SortedRows[SortedRows.Count - 1], doc).GetEndPoint(0).X); rights.Add(SortedRows[SortedRows.Count - 1]); foreach (ElementId eid in SortedRows) { diam = doc.GetElement(eid).get_Parameter(BuiltInParameter.RBS_CONDUIT_DIAMETER_PARAM).AsValueString(); if (diam == prev) { typcount += 1; } else { if (typcount != 1) { diameters = diameters + typcount + "-" + prev + "C, "; diameters = BreakLine(diameters, Store.mod_split); typcount = 1; } else if (prev != "") { diameters = diameters + prev + "C, "; diameters = BreakLine(diameters, Store.mod_split); } prev = diam; } } if (typcount != 1) { diameters = diameters + typcount + "-" + prev + "C"; } else { diameters = diameters + prev + "C"; } } ElementId firstEid = lefts[leftXs.IndexOf(leftXs.Max())]; ElementId lastEid = rights[rightXs.IndexOf(rightXs.Min())]; LocationCurve firstP = doc.GetElement(firstEid).Location as LocationCurve; LocationCurve lastP = doc.GetElement(lastEid).Location as LocationCurve; XYZ rotP = firstP.Curve.GetEndPoint(0); Distance = Math.Abs(RackDim.GetRotatedToVert(firstEid, doc, rotP).GetEndPoint(0).X - RackDim.GetRotatedToVert(lastEid, doc, rotP).GetEndPoint(0).X); Line firstL = firstP.Curve as Line; if (updateRack) { toUpdate.Text = diameters; } else { Line texRot = Line.CreateBound(firstP.Curve.GetEndPoint(0), firstP.Curve.GetEndPoint(1)); Double textAngle = texRot.Direction.AngleTo(new XYZ(0, -1, 0)); bool fix = false; if (textAngle >= Math.PI / 2) { textAngle = Math.PI - textAngle; fix = true; } TextNoteOptions textRotate = new TextNoteOptions { TypeId = defTextType, Rotation = textAngle }; TextNote text = TextNote.Create(doc, doc.ActiveView.Id, LineDir.Evaluate(Store.mod_place, true), diameters, textRotate); text.get_Parameter(BuiltInParameter.TEXT_ALIGN_HORZ).Set((Int32)TextAlignFlags.TEF_ALIGN_BOTTOM); if (fix) { text.Coord = firstP.Curve.Evaluate(Store.mod_place, true) .Add(RackDim.GetPerpendicular(firstL, Store.mod_place) .Direction.Multiply(Distance + (4 * Store.mod_firsty))); } else { text.Coord = firstP.Curve.Evaluate(Store.mod_place, true) .Subtract(RackDim.GetPerpendicular(firstL, Store.mod_place) .Direction.Multiply(Distance + (4 * Store.mod_firsty))); } text.Coord = new XYZ(text.Coord.X, text.Coord.Y + (1.4 * Store.mod_stepy), text.Coord.Z); text.AddLeader(TextNoteLeaderTypes.TNLT_STRAIGHT_R); text.AddLeader(TextNoteLeaderTypes.TNLT_STRAIGHT_R); IList <Leader> leaders = text.GetLeaders(); leaders[0].End = firstP.Curve.Evaluate(Store.mod_place, true); if (firstL.Direction.AngleTo(new XYZ(0, 1, 0)) >= Math.PI / 2) { leaders[1].End = leaders[0].End.Subtract(RackDim.GetPerpendicular(firstL, Store.mod_place) .Direction.Multiply(Distance)); } else { leaders[1].End = leaders[0].End.Add(RackDim.GetPerpendicular(firstL, Store.mod_place) .Direction.Multiply(Distance)); } if (fix) { leaders[0].Elbow = leaders[0].End.Subtract(RackDim.GetPerpendicular(firstL, Store.mod_place) .Direction.Multiply(1.4 * Store.mod_left)); leaders[1].Elbow = leaders[1].End.Add(RackDim.GetPerpendicular(firstL, Store.mod_place) .Direction.Multiply(1.4 * Store.mod_left)); } else { leaders[0].Elbow = leaders[0].End.Add(RackDim.GetPerpendicular(firstL, Store.mod_place) .Direction.Multiply(1.4 * Store.mod_left)); leaders[1].Elbow = leaders[1].End.Subtract(RackDim.GetPerpendicular(firstL, Store.mod_place) .Direction.Multiply(1.4 * Store.mod_left)); } } tx.Commit(); } return(Result.Succeeded); }
//Not working? Is this any good? public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Application app = uiapp.Application; Document doc = uidoc.Document; Selection SelectedObjs = uidoc.Selection; ICollection <ElementId> ids = uidoc.Selection.GetElementIds(); Line dimLine = null; List <XYZ> dimpos = new List <XYZ>(); List <Dimension> dimlist = new List <Dimension>(); ReferenceArray updatedref = new ReferenceArray(); ReferenceArray cleanref = new ReferenceArray(); RackDim.GetMenuValues(uiapp); foreach (ElementId eid in ids) { Element elem = doc.GetElement(eid); if (elem.GetType() == typeof(Dimension)) { Dimension dim = elem as Dimension; dimlist.Add(dim); if (dimLine == null) { dimLine = dim.Curve as Line; } foreach (Reference refe in dim.References) { if (doc.GetElement(refe.ElementId).GetType() == typeof(Grid)) { Grid grid = doc.GetElement(refe.ElementId) as Grid; Reference refgrid = new Reference(grid); updatedref.Append(refgrid); } else { GeometryElement geom = doc.GetElement(refe.ElementId).get_Geometry(Store.Dimop(doc.ActiveView)); Line refLine = RackDim.GetLineOfGeom(geom); updatedref.Append(refLine.Reference); } } } } List <Dimension> dimsort = dimlist.OrderBy(o => o.Origin.X).ToList(); foreach (Dimension dim in dimsort) { foreach (DimensionSegment dimSeg in dim.Segments) { dimpos.Add(dimSeg.LeaderEndPosition); } } using (Transaction trans = new Transaction(doc)) { trans.Start("Unite Dimensions"); Dimension newDim = doc.Create.NewDimension(doc.ActiveView, dimLine, updatedref); int segCount = 0; foreach (DimensionSegment segment in newDim.Segments) { bool once = true; foreach (Reference upref in updatedref) { if (once && newDim.References.get_Item(segCount).ElementId == upref.ElementId) { cleanref.Append(upref); once = false; } } if (segment.Value == 0) { segCount += 1; } segCount += 1; } doc.Delete(newDim.Id); newDim = doc.Create.NewDimension(doc.ActiveView, dimLine, cleanref); int iter = 0; foreach (DimensionSegment seg in newDim.Segments) { if (iter < newDim.Segments.Size - 1) { seg.LeaderEndPosition = dimpos[iter]; iter += 1; } } trans.Commit(); } return(Result.Succeeded); }
//needs grid, and only MEP elements, this way its quite limited public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Application app = uiapp.Application; Document doc = uidoc.Document; Selection SelectedObjs = uidoc.Selection; ICollection <ElementId> ids = uidoc.Selection.GetElementIds(); Line dimDirCross = null; Line dimDir = null; int selectdim = 0; bool update = false; ElementArray dimensions = new ElementArray(); ReferenceArray dimto = new ReferenceArray(); RackDim.GetMenuValues(uiapp); foreach (ElementId eid in ids) { if (doc.GetElement(eid).GetType() == typeof(Grid)) { Grid grid = doc.GetElement(eid) as Grid; dimto.Append(new Reference(grid)); } else if (doc.GetElement(eid).GetType() == typeof(Dimension)) { update = true; dimensions.Append(doc.GetElement(eid)); } else { GeometryElement geom = doc.GetElement(eid).get_Geometry(Store.Dimop(doc.ActiveView)); Line refLine = RackDim.GetLineOfGeom(geom); if (selectdim == 0) { dimDir = Line.CreateBound(refLine.GetEndPoint(0), refLine.GetEndPoint(1)); dimDirCross = RackDim.GetPerpendicular(dimDir, Store.mod_place); selectdim = 1; } dimto.Append(refLine.Reference); } } if (update) { selectdim = 0; foreach (Element elem in dimensions) { Dimension dim = elem as Dimension; ReferenceArray updatedref = new ReferenceArray(); foreach (Reference refe in dim.References) { if (doc.GetElement(refe.ElementId).GetType() == typeof(Grid)) { Grid grid = doc.GetElement(refe.ElementId) as Grid; updatedref.Append(new Reference(grid)); } else { GeometryElement geom = doc.GetElement(refe.ElementId).get_Geometry(Store.Dimop(doc.ActiveView)); Line refLine = RackDim.GetLineOfGeom(geom); if (selectdim == 0) { dimDir = Line.CreateBound(refLine.GetEndPoint(0), refLine.GetEndPoint(1)); dimDirCross = RackDim.GetPerpendicular(dimDir, Store.mod_place); selectdim = 1; } updatedref.Append(refLine.Reference); } } RackDim.CreateLinearDim(doc, uiapp, dimDir, dimDirCross, updatedref); using (Transaction deltrans = new Transaction(doc)) { deltrans.Start("Update Dimension"); doc.Delete(elem.Id); deltrans.Commit(); } } } else { RackDim.CreateLinearDim(doc, uiapp, dimDir, dimDirCross, dimto); } return(Result.Succeeded); }