Add coincident constraint to 'existing' sketchLines (and arcs)

Add coincident constraint to 'existing' sketchLines (and arcs)

sanganaksakha
Advocate Advocate
364 Views
3 Replies
Message 1 of 4

Add coincident constraint to 'existing' sketchLines (and arcs)

sanganaksakha
Advocate
Advocate

Hello,

 

I have a planarsketch that contains a bunch of unconstrained SketchLines and SketchArcs,  They form a closed curve.
Is there a way, using code (VBA / VB / iLogic), to add coincident constraints to end points of all these lines.
Is there better alternative other than using 'brute force' method (investigating each line, finding common sketchpoint with any other line etc)?

0 Likes
Accepted solutions (2)
365 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor

Hi @sanganaksakha.  There is actually a manual tool that you could likely use for that type of scenario.  Review the following link from the online help area.

To Apply 2D Sketch Dimensions Automatically 

WCrihfield_0-1722623983237.png

You can uncheck the dimensions checkbox, and only check the constraints one.  And to help dictate which types will be applied, you can use the 'Constraint Settings' tool, just two buttons below that tool button.

To Specify Geometric Constraint Default Behavior 

WCrihfield_1-1722624157324.png 

WCrihfield_2-1722624277205.png

If attempting to do all of that by code would likely require a massive code, due to how many different types of sketch objects & types there can be, and all their differences in object properties.  I think I know the names of those two commands, but I think they just launch those two user interface dialogs, rather than actually do something to an active sketch, without showing any dialog.

SketchConstraintInferScopeCmd
SketchConstraintSettingsCmd

 

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 4

Curtis_Waguespack
Consultant
Consultant
Accepted solution
0 Likes
Message 4 of 4

WCrihfield
Mentor
Mentor
Accepted solution

Inspired by the example process being used in that article linked to above, where only the one object type is being dealt with (SketchPoint), but wanting to find a different, and possibly better, or more efficient way to do it than using the PartComponentDefinition.FindUsingPoint method for finding other SketchPoint objects, I decided to give this a try myself, because it is interesting and potentially useful to me.  In this example, I also expect a PlanarSketch to be 'active', to keep it relatively simple, however I also include some error proofing and feedback for when that is not the case.  Then, since we already have a SketchPoints collection specifically for this active sketch, I simply decided to iterate that collection a second time for each SketchPoint, to keep it simple.  Due to the method being used (Merge) in this loop, and the nature of how these types of iterations work, it sometimes encounters a SketchPoint where accessing its SketchPoint.Geometry property will throw an error, so I incorporated a couple Try...Catch...End Try statements in there to avoid those occasions, for error proofing.  All my tests with this code on unconstrained sketches so far have been successful, so it seems to work just fine.

 

The efficiency of this process could likely be improved further if we could use a secondary collection for the inner iteration that got the outer iteration's SketchPoint removed from it each time, but I'm not sure if that is possible, since the collection of an iteration only gets evaluated once (before the iteration starts).  I have not done comparison processing time studies on multiple ways of doing this process, to see which one is faster, but I suspect that unless your sketch has thousands of SketchPoints in it, there likely won't be much noticeable difference.

Just throwing this one out there too.  They say variety is the spice of life after all.

Dim oActiveObj = ThisApplication.ActiveEditObject
If oActiveObj Is Nothing OrElse (Not TypeOf oActiveObj Is PlanarSketch) Then
	MsgBox("You must be in 'Edit Mode' of a PlanarSketch for this rule to work!", vbCritical, "iLogic")
	Return
End If
Dim oPSketch As PlanarSketch = oActiveObj
For Each oSPoint As SketchPoint In oPSketch.SketchPoints
	Dim oP2d As Point2d = Nothing
	Try : oP2d = oSPoint.Geometry : Catch : Continue For : End Try
	If oP2d Is Nothing Then Continue For
	For Each oOtherSPoint As SketchPoint In oPSketch.SketchPoints
		If oOtherSPoint Is oSPoint Then Continue For
		Dim oP2d2 As Point2d = Nothing
		Try : oP2d2 = oOtherSPoint.Geometry : Catch : Continue For : End Try
		If oP2d2 Is Nothing Then Continue For
		If oP2d2.IsEqualTo(oP2d, 0.001) Then
			oOtherSPoint.Merge(oSPoint)
		End If
	Next oOtherSPoint
Next oSPoint

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)