I am trying to get the line string information from a curve primitive type of line string, I am using the primitive to get the length of the line string and using other methods without any issues.
Can someone tell me what I am doing wrong?
.
Thank you
Donna
if (cPrim.GetType().Equals(CurvePrimitive.CurvePrimitiveType.LineString)) { System.Collections.Generic.IList<DPoint3d> ptList = new System.Collections.Generic.List<DPoint3d>(); if (cPrim.TryGetLineString(ptList)) { // quick check to determine if code is working double segmentDistance = ptList.ElementAt(0).Distance(ptList.ElementAt(1)); System.Windows.Forms.MessageBox.Show("ptList retrieved - " + segmentDistance.ToString()); } }
Hi Donna,
Donna Rodrick said:I am trying to get the line string information from a curve primitive type of line string
It's not quite clear how you received the curve primitive (from element using query class, also what "line string information" are you interested in?
Some information can be retrieved directly from CurvePrimitive generally (or from CurveVector when an element is more complicated), some can be obtain explicitly depending on CurvePrimitive type. So far your approach looks fine to me, only it's overcomplicated and does not look too "C#".
A simple example:
// Obtain or create CurvePrimitive List<DPoint3d> newLineVertices = new List<DPoint3d>() { new DPoint3d(0, 0, 0), new DPoint3d(10, 0, 0), new DPoint3d(10, 5, 0), new DPoint3d(5, 5, 0), }; CurvePrimitive primitive = CurvePrimitive.CreateLineString(newLineVertices); // Get native features of CurvePrimitive double length = default(double); if (primitive.Length(out length)) { MessageCenter.Instance.StatusMessage = $"Line string length is: {length:F3}"; } // Get CurvePrimitive type (LineString) specific features List<DPoint3d> receivedVertices = new List<DPoint3d>(); if (CurvePrimitive.CurvePrimitiveType.LineString == primitive.GetCurvePrimitiveType() && primitive.TryGetLineString(receivedVertices)) { MessageCenter.Instance.StatusMessage = $"Second vertext is: {receivedVertices[1]}"; }
With regards,
Jan
Bentley Accredited Developer: iTwin Platform - AssociateLabyrinth Technology | dev.notes() | cad.point
Answer Verified By: Donna Rodrick
Hi Jan
Thank you! I used IList instead of List in my declaration, I changed it to List and it works.
I use CurvePathQuery and CurveVector in my code to get most of the information that I need. However, I need to add to the line string that is part of a complex chain, so I need the vertices of the line string.
CurvePathQuery q = CurvePathQuery.GetAsCurvePathQuery(element); // Check that this element supports CurvePathQuery if (null != q) { CurveVector vec = q.GetCurveVector(); // Check that we can extract a CurveVector if (null != vec && vec.IsOpenPath) { int vecCount = vec.Count;
Donna Rodrick said:I used IList instead of List in my declaration, I changed it to List and it works.
It's strange, because to use IList is fine. I tried to define variable as IList and my code still works.
Donna Rodrick said:I use CurvePathQuery and CurveVector in my code to get most of the information that I need.
It's I guess the right way and it follow best practices.
But your code still seems to me too complicated, does not using all C# features (plus nested if conditions are usually treated as dirty code). What about this (not any testing done):
CurvePathQuery q = CurvePathQuery.GetAsCurvePathQuery(element); CurveVector vec = q?.GetCurveVector(); if (null != vec && vec.IsOpenPath) { int vecCount = vec.Count; }
I guess it can be simplified even more, because static method, doing casting to CurvePathQuery automatically, is available also:
CurveVector vec = CurvePathQuery.ElementToCurveVector(element); if (null != vec && vec.IsOpenPath) { int vecCount = vec.Count; }