DbLine
class from the namespace of all the primitives Multicad.DatabaseServices.StandardObjects
.DbLine
objects as properties contain a start and end point, but do not contain information about the length of the segment. Of course, the coordinates of the points of the segment are enough to calculate its length, but it will be more convenient to use its geometric representation — an object of the class LineSeg3d
(accessed by the DbLine.Line
property) and its Length
property to get the length: double length = line.Line.Length;
SelectObjects
method of the SelectObjects
object manager class will be used: public static McObjectId[] SelectObjects(string sPromt);
CommandMethod("LineLengthSum", CommandFlags.NoCheck | CommandFlags.NoPrefix)] public void LineLengthSum() { // McObjectId[] idSelecteds = McObjectManager.SelectObjects(" "); if (idSelecteds.Length == 0) { MessageBox.Show(" "); return; } double lengthSum = 0; foreach (McObjectId currID in idSelecteds) { // ID McObject currObj = currID.GetObject(); // if (currObj is DbLine) { lengthSum += (currObj as DbLine).Line.Length; } } MessageBox.Show(lengthSum.ToString(), " :", MessageBoxButtons.OK, MessageBoxIcon.Information); }
Polyline3d
class through the Polyline
property: double length = polyline.Polyline.Length;
ObjectFilter filter = ObjectFilter.Create(true); filter.AddLayer(" "); filter.AddType(typeof(DbLine)); List<McObjectId> ids = filter.GetObjects();
[CommandMethod("createReport", CommandFlags.NoCheck | CommandFlags.NoPrefix)] public void createReport() { List<String> reportStrings = getLengthSumByLayer(); if (reportStrings.Count == 0) { MessageBox.Show(" "); return; } // int indent = 0; // DbText caption = new DbText(); caption.Text = new TextGeom(" ", new Point3d(0, indent, 0), Vector3d.XAxis, "Standard", 10); caption.DbEntity.AddToCurrentDocument(); foreach (String str in reportStrings) { indent -= 10; DbText reportText = new DbText(); reportText.Text = new TextGeom(str, new Point3d(0, indent, 0), Vector3d.XAxis, "Standard", 6); reportText.DbEntity.AddToCurrentDocument(); } }
getLengthSumByLayer()
method, the code of which is presented below: public List<String> getLengthSumByLayer() { List<String> reportStrings = new List<String>(); // List<string> layers = McObjectManager.CurrentStyle.GetLayers(); foreach (string layerName in layers) { ObjectFilter filter = ObjectFilter.Create(true).AddType(typeof(DbLine)).AddType(typeof(DbPolyline)).AddLayer(layerName); List<McObjectId> idSelected = filter.GetObjects(); if (idSelected.Count != 0) { double lengthSum = 0; foreach (McObjectId currID in idSelected) { // ID McObject currObj = currID.GetObject(); // if (currObj is DbLine) { lengthSum += (currObj as DbLine).Line.Length; } else if (currObj is DbPolyline) { lengthSum += (currObj as DbPolyline).Polyline.Length; } } // , if (lengthSum != 0) { reportStrings.Add(layerName.ToString() + ": " + lengthSum.ToString()); } } } return reportStrings; }
Source: https://habr.com/ru/post/246511/
All Articles