Change sketch constraints programmatically [iLogic]

Change sketch constraints programmatically [iLogic]

Anonymous
Not applicable
1,248 Views
5 Replies
Message 1 of 6

Change sketch constraints programmatically [iLogic]

Anonymous
Not applicable

Capture_SketchConstraints.PNG

 

I have two reference lines, with different angles, 4 deg & 10 deg. The first line has length of KnuckleDist = 1000.

A rectangle shape is constrained to PlateDistance = 800mm from origin, and one side of the rectangle has a Collinear Constraint to the first reference line.

 

I would like to programmatically re-constrain the rectangle with Collinear Constraint to first Reference Line if KnuckleDistance > PlateDistance and to second Reference Line if KnuckleDistance < PlateDistance (so that the rectangle is always resting on the Reference Line beneath it).

 

Can this even be done with iLogic?

0 Likes
Accepted solutions (1)
1,249 Views
5 Replies
Replies (5)
Message 2 of 6

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @Anonymous 

It's possible for sure. Can be difficult though depending on the complexity of your sketch. You need to figure out how to identify the lines. I made a simple example part for you with a rule in it that toggles a collinear constraint between two differrent reference lines. See attached ipt.

Message 3 of 6

Anonymous
Not applicable

@JhoelForshav Thanks, but I am unable to open the file (Using Inventor 2019). Could you please paste the code, and I'll hopefully be able to piece it together?

0 Likes
Message 4 of 6

JhoelForshav
Mentor
Mentor

Hi @Kenny.P 

Too bad you couldn't open the file. The code is and has to be very specific to the sketch entities within the sketch. As i said, it can be a bit tricky to identify the sketchlines you want to use...

Anyways, here goes:

Dim oDoc As PartDocument = ThisDoc.Document
Dim oSketch As PlanarSketch = oDoc.ComponentDefinition.Sketches.Item("Sketch2")
Dim oLine As SketchLine
Dim oPrevRefLine As SketchLine
For Each oConstraint As GeometricConstraint In oSketch.GeometricConstraints
 If oConstraint.Type = ObjectTypeEnum.kCollinearConstraintObject
  oLine = oConstraint.EntityOne
  oPrevRefLine = oConstraint.EntityTwo
  oConstraint.Delete
  Exit For
 End If
Next
For Each sLine As SketchLine In oSketch.SketchLines
 If sLine.Reference AndAlso sLine IsNot oPrevRefLine
  oSketch.GeometricConstraints.AddCollinear(oLine, sLine)
  Exit For
 End If
Next

iLogicVB.UpdateWhenDone = True

 

 
Message 5 of 6

Anonymous
Not applicable

Thank you @JhoelForshav , the code makes sense, and the clip is helpful!

 

I think what I will need to do is write a separate rule that returns the ReferenceKey of a selected SketchLine, and save hardcoded ReferenceKeys of the three relevant SketchLines in my code.

 

Then at runtime, I can use the ReferenceKeys to find which constraints to remove, and to create new constraints.

 

It seems a bit tricky, but I think it should work.

0 Likes
Message 6 of 6

Anonymous
Not applicable

So with help from @JhoelForshav's code, I got it working the way I wanted.

Posting my code here in case others have use for it. (I found many people asking similar questions)

 

 

' Select a SketchLine in the window and run this rule to get the RefKey
Class GetSketchLineRefKey
	Sub Main
		Dim oDoc As PartDocument = ThisDoc.Document
		Dim oSketchLine As SketchLine = oDoc.SelectSet(1)

		Dim refKey() As Byte
		refKey = New Byte() {}

		Call oSketchLine.GetReferenceKey(refKey)

		Dim strKey As String
		strKey = oDoc.ReferenceKeyManager.KeyToString(refKey)
		Logger.Info("Line Refkey: " & strKey)
	End Sub
End Class

 

 

Then replace the RefKey Strings in rule below with the ones you receive from previous code:

 

Class ReconstrainSketch
	Dim oDoc As PartDocument
	Sub Main
		oDoc = ThisDoc.Document
		Dim oSketch As PlanarSketch = oDoc.ComponentDefinition.Sketches.Item("Sketch2")
		
		'[ Enter RefKey for lines from SketchLineRefKey rule strings manually
		Dim oMovingLine As SketchLine	= RefKeyStringToObject("EAIAABAAAABSAAAAAAAAAIAAAAAAAAAA")
		Dim oRefLine1 As SketchLine 	= RefKeyStringToObject("EAIAABAAAAAXAQAAAAAAAIAAAAAAAAAA")
		Dim oRefLine2 As SketchLine		= RefKeyStringToObject("EAIAABAAAAAwAQAAAAAAAIAAAAAAAAAA")
		']
		
		For Each oConstraint In oMovingLine.Constraints
			If (oConstraint.Type = ObjectTypeEnum.kCollinearConstraintObject)
				Dim oLine1 As SketchLine = oConstraint.EntityOne
				Dim oLine2 As SketchLine = oConstraint.EntityTwo
				
				If (KnuckleDistance < PlateDistance) And ((oLine1 Is oMovingLine And oLine2 Is oRefLine1) Or (oLine1 Is oRefLine1 And oLine2 Is oMovingLine)) Then
					Logger.Info("Reconstraining 1")
					oConstraint.Delete
					oSketch.GeometricConstraints.AddCollinear(oMovingLine, oRefLine2)
					Exit For
				Else If (KnuckleDistance > PlateDistance) And ((oLine1 Is oMovingLine And oLine2 Is oRefLine2) Or (oLine1 Is oRefLine2 And oLine2 Is oMovingLine)) Then
					Logger.Info("Reconstraining 2")
					oConstraint.Delete
					oSketch.GeometricConstraints.AddCollinear(oMovingLine, oRefLine1)
					Exit For
				End If
				
			End If
		Next


	iLogicVb.UpdateWhenDone = True

	End Sub
	
	Function RefKeyStringToObject(strKey As String) As Object
		Dim refKey() As Byte
		refKey = New Byte() {}
		Call oDoc.ReferenceKeyManager.StringToKey(strKey, refKey)
		Dim obj As Object = oDoc.ReferenceKeyManager.BindKeyToObject(refKey)
		Return obj
	End Function

End Class

 

 

I'm not fond if the following code:

If (KnuckleDistance < PlateDistance) And ((oLine1 Is oMovingLine And oLine2 Is oRefLine1) Or (oLine1 Is oRefLine1 And oLine2 Is oMovingLine))

but I found no better way at the moment.