Accessing a constraint by name in Ilogic

Accessing a constraint by name in Ilogic

Thomas.Long
Advocate Advocate
904 Views
2 Replies
Message 1 of 3

Accessing a constraint by name in Ilogic

Thomas.Long
Advocate
Advocate

I'm curious if it's possible to access a constraint by name with the new Ilogic API? 

 

My current attempt gives the following error:

 

 

MsgBox(Constraints("Mate:25").ToString())

 

 

ThomasLong_0-1690997707007.png

 

 

The code here isn't meant to do anything other than let me know if I can access it by name. I have a program that generates assemblies with a known number of parts (known for each assembly, but this changes for every instance of the assembly and it has basically an infinite number of permutations, which is why I generate new assemblies rather than using one with a basic setup)

 

Currently I don't add a name, but below is the code where I constrain each new part. The basic concept is that if I later change my parameters, I should be able to run the overhead function again, find the constraints by name for each part based off that part name and tell it "This should be a mate. If it is, leave it. If it isn't, change it to a mate."

 

Does anyone know how to access the constraint through the new Constraints API or do I need to go back into ThisDoc.Document.ComponentDefinition.Constraints in order to access it? My preference would be to use my generation function and the creation function at the same time (i.e. check if it exists, if yes then update, if no then create)

 

 

'Creates constraints for picked faces off of the base planes for the file
Function CreateConstraints(DConstraintType, DConstraint, AConstraintType, AConstraint, DepthConstraintType, DepthConstraint, Orientation)

	'Declare Document Variables	
	Dim oAssemblyDef=ThisDoc.Document.ComponentDefinition 
	Dim CompOcc As ComponentOccurrence = oAssemblyDef.Occurrences.Item(oAssemblyDef.Occurrences.Count)
	
	Dim currentPlane As String

	If DepthConstraintType = "Mate" Then Constraints.AddMate("", "", "XY Plane", CompOcc.Name, "XZ Plane", DepthConstraint)
	If DepthConstraintType = "Flush" Then Constraints.AddFlush("", "", "XY Plane", CompOcc.Name, "XZ Plane", DepthConstraint)

	If Orientation = "Vertical" Then 
		currentPlane = "XY Plane"
	Else
		currentPlane = "YZ Plane"
	End If

	'Constrain to XZ Plane
	If AConstraintType = "Mate" Then Constraints.AddMate("", "", "XZ Plane", CompOcc.Name, currentPlane, AConstraint)
	If AConstraintType = "Flush" Then Constraints.AddFlush("", "", "XZ Plane", CompOcc.Name, currentPlane, AConstraint)
	
	If Orientation = "Vertical" Then 
		currentPlane = "YZ Plane"
	Else
		currentPlane = "XY Plane"
	End If

	'Constrain to YZ Plane
	If DConstraintType = "Mate" Then Constraints.AddMate("", "", "YZ Plane", CompOcc.Name, currentPlane, DConstraint)
	If DConstraintType = "Flush" Then Constraints.AddFlush("", "", "YZ Plane", CompOcc.Name, currentPlane, DConstraint)
End Function

 

 

Edit: Also, is it possible to check if the constraint exists without just writing a for each loop? Right now if I try to access it by name and it doesn't exist it just throws an error but I don't see a function to check if the overall ThisDoc.Document.ComponentDefinition.Constraints actually contains the constraints so I could just tell it to check beforehand. Perhaps something to do with the IQueryable conversion?

 

Thank you in advance!

0 Likes
Accepted solutions (1)
905 Views
2 Replies
Replies (2)
Message 2 of 3

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @Thomas.Long 

 

Since you are handing the component to the Function, I think you can look at the constraints collection for that component and get the name.

 

Something like this ( although this example is using a selection set to get the component occurrence)

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Dim sSet As SelectSet = ThisDoc.Document.SelectSet

If sSet.Count = 0 Then 
	MsgBox("Select an occurrence first.")
	Exit Sub
End If

Dim CompOcc = Component.InventorComponent( sSet.Item(1).name)

oConstraints = CompOcc.Constraints

FistConstraintname = oConstraints.item(1).name

MsgBox(FistConstraintname)

  

 

Message 3 of 3

Thomas.Long
Advocate
Advocate

This isn't exactly what I was looking for but it limits my iterations so much I don't need to worry anymore! My biggest concern was assemblies where I had a few hundred components and I would have to iterate through 600+ constraints over and over again, but this really cuts down on the that!

 

Honestly, it probably works better than what I expected since I'm limiting the search by component.

 

Thank you!