Adding and constraining components in assembly

Adding and constraining components in assembly

tomislav.peran
Advocate Advocate
1,231 Views
5 Replies
Message 1 of 6

Adding and constraining components in assembly

tomislav.peran
Advocate
Advocate

Hello,

 

So maybe it is better for something like this to use Factory Design add-in but I do not really like using that in 3D so I am trying to use the rule written below as a substitute for here and there. 

 

In the same folder, I have my assy named "My Assy" and the component that I want to add, named "Cube".

Firstly Cube:1 is added and constrained in the correct place, then I start the code.

By using the code new cubes are added and the constraint "insert" is automatically activated (also with a locked rotation so no additional constraints are needed).

 

My problem is that I want to use a named entity when adding a constraint. The named entity is an edge that is defined in a Cube. Second edge I pick myself (one of the holes).

Since new cubes are added I think this code does not understand that it needs to pick a named entity from the last Cube that is added. But I am guessing here being a starter with this coding.

 

If anyone knows how to solve this please let me know.

 

Dim oAsm As AssemblyDocument = ThisDoc.Document
Dim oDoc As PartDocument
Dim oPath As String
oPath = ThisDoc.Path
Dim oName As String
oName = "Cube.ipt"

oDoc = ThisApplication.Documents.Open(oPath & "\" & oName, False)
oDoc.Close

Try
i = oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count + 1
Catch
i = 1
End Try

While True

Dim componentA = Components.Add("Cube:" & i, oPath & "\" & oName, position := Nothing, grounded := False, visible := True, appearance := Nothing)

Dim iLogicAuto = iLogicVb.Automation
Dim namedEntities = iLogicAuto.GetNamedEntities(oDoc)
Dim oEdge1 As Edge = namedEntities.FindEntity("My Edge")

Dim oEdge2 As Edge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPublicationEdgeFilter, "Pick second edge. press Esc to cancel")
If oEdge2 Is Nothing Then Exit Sub

oAsm.ComponentDefinition.Constraints.AddInsertConstraint2(oEdge1,oEdge2,True,0,True)
oAsm.Update

	i = i + 1

End While

tomislavperan_0-1654853158816.pngtomislavperan_1-1654853290223.png

 

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

dalton98
Collaborator
Collaborator

How I would go about it is naming every face/edge geometry like you did with "My Edge". Then you could use the pick tool in the assembly to retrieve that geometry to make a constraint.

Dim oFace As FaceProxy = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "select face")

Dim oFace1 As Face
oFace1 = oFace.NativeObject

Dim component2 As String = oFace.Parent.Parent.Name
Dim component2entity As String = oFace1.AttributeSets.Item("iLogicEntityNameSet").Item("iLogicEntityName").Value

Constraints.AddMate("Mate1", "cube:1", "Face0", component2, component2entity)

 

Message 3 of 6

Curtis_Waguespack
Consultant
Consultant

Hi @tomislav.peran 

 

See this link for something similar that might give you some ideas. There is a zip file with a simple data set in that post:

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/place-component-gt-activate-insert-m...

 

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

EESignature

Message 4 of 6

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @tomislav.peran,

 

After I posted the previous link, I started to think about some of the specifics, so I took a few minutes to update the data set at that link to match your example.

 

Here is an Inventor 2022 version of this for you to look at (see the rule called Rule0 in the assembly file):

2022 Place and Constraint Cubes.zip 

 

 

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

 

And here's the code in case it helps someone in a future search:

 

 

 

Imports System.Text.RegularExpressions
Dim oDoc As AssemblyDocument
oDoc = ThisDoc.Document


Dim oCubeFile = "Cube.ipt"
Dim oCubeOccPrefix = "Cube:"
Dim oEdgePrefix = "InsertEdge_"

i = 0
'count existing cubes, if any
For Each oOcc In oDoc.ComponentDefinition.Occurrences
	If oOcc.name.contains(oCubeOccPrefix) Then
		oOcc.Name = oCubeOccPrefix & "---" & i 'set temp name
		i = i + 1
	End If
Next

i = 1
'renumber
For Each oOcc In oDoc.ComponentDefinition.Occurrences
	If oOcc.name.contains(oCubeOccPrefix) Then
		oOcc.Name = oCubeOccPrefix & i 'set  name
		i = i + 1
	End If
Next

Dim iLogicAuto = iLogicVb.Automation
Dim oEdge As Edge


While True

	oEdge = ThisApplication.CommandManager.Pick(
	SelectionFilterEnum.kPartEdgeCircularFilter,
	"Select an insert edge")

	' If nothing gets selected then we're done	
	If IsNothing(oEdge) Then Exit While

	Dim oCube = Component.InventorComponent(oEdge.Parent.Parent.name)
	Dim oNamedEntities = iLogicAuto.GetNamedEntities(oCube.Definition.Document)
	
	'get or set existing cube insert edge name
	Try
		oName = oNamedEntities.GetName(oEdge)
	Catch
	End Try
	oEdgeName = oName

	If oEdgeName = "" Then

		TryAgain :
		'create new name using date & time
		oNow = oEdgePrefix & Now()
		oEdgeName = Regex.Replace(oNow, "[:/ ]", String.Empty)

		If oNamedEntities.NameExists(oEdgeName) Then
			GoTo TryAgain
		Else
			oNamedEntities.SetName(oEdge, oEdgeName)
		End If
	End If

	'Insert new
	Dim oNewCube = Components.Add(oCubeOccPrefix & i, oCubeFile)

	'constriants
	Constraints.AddInsert("CubeInsert:" & i, 
	oNewCube, "Protrusion_Edge", 
	oCube.Name,	oEdgeName, axesOpposed := True)
	
	Constraints.AddFlush("CubeFlush:" & i, 
	oNewCube, "XZ Plane",
	oCube.Name,	"XZ Plane")

	InventorVb.DocumentUpdate()

	i = i + 1
	
'	ThisApplication.ActiveView.Fit
End While



 

 

 

 

 

 

 

 

EESignature

Message 5 of 6

tomislav.peran
Advocate
Advocate

Thanks a lot, guys! I will look at it tommorow when I get access to Inventor.  

 

In the video, the code does exactly what I had in mind. 🙂

 

Message 6 of 6

tomislav.peran
Advocate
Advocate

Hi Curtis,

 

It has been a while since I posted this question but I have opened recently this rule and I am wondering about one thing.

 

In the following part of the code is it necessary to start with i=0 ? Would not result be the same if we started with i=1 immediately? 

 

i = 0
'count existing cubes, if any
For Each oOcc In oDoc.ComponentDefinition.Occurrences
	If oOcc.name.contains(oCubeOccPrefix) Then
		oOcc.Name = oCubeOccPrefix & "---" & i 'set temp name
		i = i + 1
	End If
Next

i = 1
'renumber
For Each oOcc In oDoc.ComponentDefinition.Occurrences
	If oOcc.name.contains(oCubeOccPrefix) Then
		oOcc.Name = oCubeOccPrefix & i 'set  name
		i = i + 1
	End If
Next

Tom

0 Likes