Place component -> activate insert mate automatically

Place component -> activate insert mate automatically

tomislav.peran
Advocate Advocate
2,818 Views
14 Replies
Message 1 of 15

Place component -> activate insert mate automatically

tomislav.peran
Advocate
Advocate

Hello,

 

I am not sure if this is possible but I will give it try asking. 

 

Lets say I have a component with some holes.

I want to add screws into some of those holes but I do not want to use iMates to position those screws (sometimes that is not easy if there are too many holes to choose from).

 

Instead I would like to activate a rule which does the following:

 

1. Inserts desired screw -> for that I can use already created snippet inside Inventor. Screw is saved somewhere locally.

 

2. After inserting a screw I want to activate "Insert" command automatically so that I can choose edges to mate.  

Even better if the desired edge of the screw (that one is always the same) becomes immediately selected so that I only have to choose the edge of the hole.

 

3. Repeat command until ESC is clicked -> I think this can be done with "while loop" but I just started learning more about loops in  iLogic so I am not sure if that is the best way. 

 

Does anyone knows if the second step is possible at all? 

 

Thanks,

Tom

Accepted solutions (2)
2,819 Views
14 Replies
Replies (14)
Message 2 of 15

tomislav.peran
Advocate
Advocate

Hello,

 

Little update. It is possible. Seems that this part of the code is working. 

 

Dim componentA = Components.Add("Screw", "Location of the part", position := Nothing, grounded := False, visible := True, appearance := Nothing)

Dim oEdge1 As Edge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPublicationEdgeFilter, "Pick first edge. press Esc to cancel")
If oEdge1 Is Nothing Then Exit Sub
Dim oEdge2 As Edge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPublicationEdgeFilter, "Pick second edge. press Esc to cancel")
If oEdge2 Is Nothing Then Exit Sub
Dim oAsm As AssemblyDocument = ThisDoc.Document
oAsm.ComponentDefinition.Constraints.AddInsertConstraint(oEdge1,oEdge2,True,0)
oAsm.Update

However, I can only run rule once. Second time screw does not appear.
I am also curious if edge od the screw can be automatically selected.

If anyone has an idea please let me know,

Cheers,
Tom

 

0 Likes
Message 3 of 15

dalton98
Collaborator
Collaborator

"Screw" is too unique you would have to use "Screw:1" "Screw:2" etc.

Is there a reason you are not using content center and the Bolted Connection api?

0 Likes
Message 4 of 15

tomislav.peran
Advocate
Advocate

Hi Dalton,

 

I am using screws just as an simple example. However, there are components that I have to add sometimes in a large quantity. So I am thinking ways of how to do it...

 

Insert mate is the fastest way to mate things if applicable...so I am digging in that direction.

 

0 Likes
Message 5 of 15

dalton98
Collaborator
Collaborator

Oh ok. I know you said you didn't want to use imates but that would be the easiest way to do it. If you want to stick with how your doing it then you would have to set it up like this:

Dim oAsm As AssemblyDocument = ThisDoc.Document
Dim oDoc As PartDocument

oDoc = ThisApplication.Documents.Open("Location of part", False)
oDoc.Close

Try i = oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count + 1
Catch
i = 1
End Try Dim componentA = Components.Add("Screw:" & i, "Location of part", position := Nothing, grounded := False, visible := True, appearance := Nothing) Dim oEdge1 As Edge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPublicationEdgeFilter, "Pick first edge. press Esc to cancel") If oEdge1 Is Nothing Then Exit Sub 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.AddInsertConstraint(oEdge1,oEdge2,True,0) oAsm.Update

 

0 Likes
Message 6 of 15

Curtis_Waguespack
Consultant
Consultant

Hi @tomislav.peran,

 

See the attached *.zip file for example files (Inventor 2019) for doing this with iLogic.

 

see this version:  PlateAssembly_2020.zip  

 

This example, places and constrains a screw part into a plate assembly, looping and prompting you to select hole edges, until you press ESC.

 

Note that in this example, the iLogic code places the screw part automatically, by finding it in the workspace. This assumes there is only one file called "Screw.ipt" and that the file is indeed in the workspace. Alternately, if the assembly has one instance of a file called "Screw.ipt" inserted already, it just uses it.

 

Here's the code too, incase that helps someone find this in a future search. (edit: this is the fixed version as mentioned in later replies)

 

 

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

 

 

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

Dim oPlate = Component.InventorComponent("Plate:1")
Dim oScrewFile = "Screw.ipt"
Dim oScrewOccPrefix = "Screw:"
Dim	oEdgePrefix = "PlateInsertEdge_"

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

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

Dim iLogicAuto = iLogicVb.Automation
Dim oNamedEntities = iLogicAuto.GetNamedEntities(oPlate.Definition.Document)
Dim oEdge As Edge


While True

	oEdge = ThisApplication.CommandManager.Pick(
	SelectionFilterEnum.kPartEdgeCircularFilter,
	"Select an edge on the plate")

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

	'filter out accidental selection of the screw edges 
	If Not oEdge.Parent.Parent.name = oPlate.Name Then Continue While

	'get or set plate 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 screw
	Dim oScrew = Components.Add(oScrewOccPrefix & i, oScrewFile)

	'constrian screw
	Constraints.AddInsert("ScrewInsert:" & i, oScrew, "InsertEdge", oPlate.Name,
	oEdgeName, axesOpposed := True)

	InventorVb.DocumentUpdate()

	i = i + 1
End While

 

 

EESignature

0 Likes
Message 7 of 15

tomislav.peran
Advocate
Advocate

Hi Dalton,

 

Thanks a lot for the code! For some reason I get the same problem as before. Rule does not want to repeat itself automatically. And I can not add component after I added it once. 

 

What I did for now is this:

1. Changed part of my starting code a bit:

 

Dim componentA = Components.Add("", "Location of the component", position := Nothing, grounded := False, visible := True, appearance := Nothing)

Dim oEdge1 As Edge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Pick first edge. press Esc to cancel")
If oEdge1 Is Nothing Then Exit Sub
Dim oEdge2 As Edge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Pick second edge. press Esc to cancel")
If oEdge2 Is Nothing Then Exit Sub
Dim oAsm As AssemblyDocument = ThisDoc.Document
oAsm.ComponentDefinition.Constraints.AddInsertConstraint(oEdge1,oEdge2,True,0)
oAsm.Update

 

SelectionFilterEnum.kPartEdgeFilter -> this seems to work better than what I used before
Dim componentA = Components.Add("" -> if I leave the name out than I am able to add component again (if I start the rule again) and the names will be generated by iLogic. That was a good tip about the names, thanks!

 

2. To repeat the rule I made a "master" rule and just added upper rule 5 times. 😄


iLogicVb.RunRule("Insert mate")

iLogicVb.RunRule("Insert mate")

iLogicVb.RunRule("Insert mate")

iLogicVb.RunRule("Insert mate")

iLogicVb.RunRule("Insert mate")


About iMates, if I have the component with two iMates that are assigned to it and too many matching mates in the assembly then it gets messy. I find it then  easier to add a component manually then wait for it to find proper position.

 

Tom

0 Likes
Message 8 of 15

tomislav.peran
Advocate
Advocate

Hi Curtis,

 

This is an amazing idea! However when I try to run the code, even though the screw is added "Insert" mate does not seems to work. 

 

I get the following error:

 

"Error on line 62 in rule: Rule0, in document: PlateAssembly.iam

Object reference not set to an instance of an object."

 

I am trying to figure it out what that means but I still miss the knowledge about it....

 

If you have any idea please let me know.

 

Tom

0 Likes
Message 9 of 15

dalton98
Collaborator
Collaborator
Accepted solution

@tomislav.peran a simple trick is to offset the imates by 1mm on both the part and the assembly. Then they will only choose that imate. I attached some example parts from a course I took on it. When you place the component select this:

then right click in the assembly to place all

daltonparrish_0-1650630008349.png

 

0 Likes
Message 10 of 15

tomislav.peran
Advocate
Advocate

Hi Dalton,

 

Oh, this works well. I would still like to pick the edges because sometimes I work with components where 2 insert mates are needed (like a rail with 2 poles) and platform where those rails come have large number of holes where poles could fit. 

 

But this is one good solution for sure. Thanks a lot!

 

 

0 Likes
Message 11 of 15

Curtis_Waguespack
Consultant
Consultant

Hi @tomislav.peran,

 

hmmmm.... this worked in Inventor 2019, but when I look at it in Inventor 2022, I see that it is not automatically creating the plate named edge as needed.  I'll have a look and post back if I can determine the issue.

 

see this version:  PlateAssembly_2020.zip  

 

 

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

EESignature

0 Likes
Message 12 of 15

Curtis_Waguespack
Consultant
Consultant

Hi @tomislav.peran 

 

Okay, it looks like Autodesk fixed something from Inventor 2019 to Inventor 2022 that caused the way I was handling an existing edge name to fail.

 

See the attached Inventor 2022 version (or let me know if you are running Inventor 2020 or 2021 and I can provide it in that version).

 

see this version:  PlateAssembly_2020.zip  

 

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

 

EESignature

0 Likes
Message 13 of 15

tomos6121
Explorer
Explorer

Hi Curtis, 

 

Thank you very much for your reply.

 

I am using Inventor 2021. On Monday when I will have access to Inventor again I will test the code. Looking forward to check it out. 

 

Tom

Message 14 of 15

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @tomislav.peran,

 

Attached is an Inventor 2020 version (incase someone else with that version would like to look at it in the future)

 

I tested it in Inventor 2021 as well, so you should be able to open this version and see how it works, but if not let me know.

 

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

 

EESignature

Message 15 of 15

tomislav.peran
Advocate
Advocate

Hi Curtis, 

 

I have tested your code and it works just fine. The code is not yet entirely clear for me but I do understand that you have used "Named entities" to pre-select the screw edge. That works great. 🙂 

 

However, once I put a second plate into the assembly the code works no more. Code is limited to only the first plate. 

 

Motivation behind my question is to try to automate the proces of inserting the standards components (such as bearings, flanges, rails, grids or such parts) which use 2 "Insert" constraints for definition once inserted into a random assembly. 

 

I guess the code that I would like to use in the end should look like this:

 

1. Insert part (ex.standard rail) into random active assembly -> rail has 2 poles which are constraint with mate "Insert"

2. Activate Insert -> choose automatically edge named "edge1" in the rail+ user picks the 2nd edge in assy

3. Activate Insert -> choose automatically edge named "edge2" in the frail + user picks the 2nd edge in assy

{ 4. Activate pattern to pattern inserted standard rail (oke, this is a bit special case that would help me a lot, but if it is to complicated to add this I can live without it. I guess here would also be used  named rails edges 3 & 4 for example to indicate direction of a pattern and user would choose the number) }

5.Repeat or Click escape 

 

Thank you a lot for the code you provided anyway, it is a fantastic work,

Tom 

 

0 Likes