Measure loop length - erratic results

Measure loop length - erratic results

Electrik
Advocate Advocate
928 Views
4 Replies
Message 1 of 5

Measure loop length - erratic results

Electrik
Advocate
Advocate

With the help of this forum, I've been able to put together a small program to measure the length of a loop, calculate length of chain (and # of links) based on that, and update properties accordingly. There's a bit more to it, but that's the basics.

At the most basic level, assuming you've named your sketch containing the chain path as "Path", these lines will find that sketch, and return the value of the loop:

		'Calculate length of sweep- returned by function in centimeters
		Dim PathSketch As SketchEntity = ThisDoc.Document.ComponentDefinition.Sketches("Path").SketchLines(1)
		PathLengthMM = ThisApplication.MeasureTools.GetLoopLength(PathSketch) * 10

 

This path can be complex, but is usually just two arcs at the end of two line segments:

rikpfau_0-1683028570864.png

 

Usually, this works, but SOMETIMES, the logic will find the length of one line segment, not the whole loop.

Yes, they are connected, full loops. The model geometry is working just fine. It's a sweep of the chain's profile.

Using the measure command, it can find the loop just fine (cycling through the selection options).

 

How can I stop this erratic behavior, and ensure it finds the length of the entire loop, not just a segment?

Inventor Professional 2021
Vault Professional 2021
0 Likes
Accepted solutions (2)
929 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor

Hi @Electrik.  When you only input a single SketchLine or SketchEntity type object, it then tries to check for other entities that are 'connected end to end' with that initial entity within the same sketch, to get the whole 'loop'.  In that case I believe the natural order of the entities originating from the original one play a role in the outcome (lines and arcs have direction, like start & end points).  And if those entities are not actually constrained end to end, that may also play a role.  If this is the case, then which entity you select along the overall profile could possibly influence the outcome of the measurement.

Maybe instead of just supplying the single sketch entity, you could supply the whole 'ProfilePath' object.  You can get ProflePath objects from the PlanarSketch.Profiles.Item(1).Item(1)...where Profiles.Item(1) is a single Profile object, then Profile.Item(1) is a single ProfilePath type object.  The ProfilePath object has a Property named 'Closed', which has a Boolean type value that you can check to see if that ProfilePath is a closed profile or not.  To get the first 'Closed' ProfilePath, you may have to loop through the Profile objects in the Sketch.Profiles, then loop through the ProfilePath objects in each Profile object, until you find a closed one.  Just some additional thoughts.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 5

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Hi @Electrik . I also sometimes need to do a measure perimeter action, and I usually use this Measure.Perimeter("sketchName"), it works great in my case.

PathLengthMM = Measure.Perimeter("Path")

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 4 of 5

WCrihfield
Mentor
Mentor

That one works pretty well too, when the sketch is simple, and primarily just contains that one profile.  Below is an example of the method I was referring to also.  It is certainly more code, but can likely work in more complex sketches which may contain more geometry than just the one profile.  In my test example, I had a sketch of a slot that I used to extrude that slot through a part, since that matches your image pretty well.  But the simpler code above worked just as well in that simple case too.

Dim oSketch As PlanarSketch = ThisDoc.Document.ComponentDefinition.Sketches.Item("Sketch6")
If oSketch.Profiles.Count = 0 Then Exit Sub
Dim oProfilePath As ProfilePath = Nothing
For Each oProfile As Profile In oSketch.Profiles
	For Each oPP As ProfilePath In oProfile
		If oPP.Closed Then oProfilePath = oPP : Exit For
	Next 'oPP
	If oProfilePath IsNot Nothing Then Exit For
Next 'oProfile
Dim dLength As Double = ThisApplication.MeasureTools.GetLoopLength(oProfilePath)
UOM = ThisDoc.Document.UnitsOfMeasure
dLength = UOM.ConvertUnits(dLength, "cm", UOM.LengthUnits)
MsgBox("Loop Length = " & dLength, vbInformation, "Loop Length")

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 5

Electrik
Advocate
Advocate
Accepted solution

Aha!!! Thank you both for your input. They didn't give me the solution, but led me there.

The Measure.Perimeter still gave an incorrect value on one sketch. It was because the Sketch called "Path" contained extra geometry, just not used in the sweep. (The solid circle, see pic)

Electrik_0-1683035835462.png

If that circle is changed to a construction line, then everything works perfectly.

Following that train of thought, we found that my original code is thrown off by even a construction line. As long as there's NO OTHER GEOMETRY in that sketch, everything's fine. As soon as you get even one construction line, the original "GetLoopLength" is thrown off, and measures only that line.

Problem solved - use Measure.Perimeter, and change reference geometry to Construction. (kind of a no-brainer, really.) Thanks again!!!

Inventor Professional 2021
Vault Professional 2021
0 Likes