iLogic to Add Component to Assembly Based on Custom iProp

iLogic to Add Component to Assembly Based on Custom iProp

Anonymous
Not applicable
2,949 Views
13 Replies
Message 1 of 14

iLogic to Add Component to Assembly Based on Custom iProp

Anonymous
Not applicable

Hello,

 

I have spent a couple of days working on automating a couple of redundant tasks in an engineering workflow using inventor iLogic. At this point I'm stumped.

 

Here is my goal:

 

From IDW:

-> Open first view assembly

-> Iterate through assembly finding all components with a custom "CNC" iProp

-> Open our assembly template

-> Save the blank template in our CNC folder

-> Add only parts with "CNC" iProp to the new CNC assembly

-> Save assembly and close

 

Basically everything works so far (mostly a Frankenstein of code found on here, cheers to all unknown contributors) besides the line that actually adds the component.

 

I went this rout because since typically 5% of parts are CNC parts, it would be faster to create a new assembly then to save a copy and delete out all of the other parts.

 

Thoughts appreciated.

 

Thanks,

Funnybus

 

 

If System.IO.File.Exists(oCNCPath & oCNCName) Then
	oFileCheck = MessageBox.Show(oCNCPath & oCNCName & " already exists." & vbLf & vbLf & "Do you want to replace it?", "Confirm Save As", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
		If oFileCheck = vbYes Then
			'CREATE CNC
		End If		
Else
	oOldDoc = ThisApplication.Documents.Open(oAsmTemp, False)
	Dim oNewPath As String= oCNCPath & oCNCName
	oOldDoc.saveas(oNewPath, True)
	oOldDoc.Close()
	Dim oNewDoc As AssemblyDocument = ThisApplication.Documents.Open(oNewPath, False)
	TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, oNewDoc.ComponentDefinition.Occurrences, 1)
	oNewDoc.Close()
End If
Private Sub TraverseAssembly(oAssyDocOccurrences As ComponentOccurrences, oNewDocOccurrences As ComponentOccurrences, Level As Integer)

Dim oOcc As ComponentOccurrence
Dim oOccName As String
Dim oCNC As String

For Each oOcc In oAssyDocOccurrences
		Try
			oCNC = oOcc.Definition.Document.PropertySets.Item("Inventor User Defined Properties").Item("CNC").Value
				If oCNC = "CNC"
					oOccName = oOcc.Definition.Document.FullFileName
					MessageBox.Show(oOccName)
					oNewDocOccurrences.Add(oOccName)
				End If
		Catch
		'No Error
		End Try
	
		If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
			Call TraverseAssembly(oOcc.SubOccurrences, oNewDocOccurrences, Level + 1)
		End If  
Next
	
End Sub

 

0 Likes
Accepted solutions (1)
2,950 Views
13 Replies
Replies (13)
Message 2 of 14

MechMachineMan
Advisor
Advisor

 

1. How are you handling quantities, or is that irrelevant?

2. Do you use a lot of reference parts in your models

2a. Is it possible that reference parts will ALSO be CNC parts?

3. How do you keep the parent and created assemblies linked?

 

4. Are you just needing to create a parts list with all of the cnc parts?

       -- If so, can I reccomend writing a rule to make a view rep of all of your cnc parts and then using a parts list filtered by view rep instead?


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 3 of 14

Anonymous
Not applicable

1. How are you handling quantities, or is that irrelevant?

 

-> Since the rule iterates through each part, a duplicate part would be added to the assembly each time it encounters one.

 

2. Do you use a lot of reference parts in your models

 

-> We never use virtual parts, and to date, I have never seen any of our parts with a reference BOM structure. 

 

2a. Is it possible that reference parts will ALSO be CNC parts?

 

-> Looking into the future is impossible at this place, but I would guess probably not.

 

3. How do you keep the parent and created assemblies linked?

 

-> The way this is set up, the rule would replace the CNC assembly each time the IDW is pushed into production.

 

4. Are you just needing to create a parts list with all of the cnc parts?

 

-> The purpose of this is to create an assembly using only CNC parts for our 3D nesting & CNC programs to use. Our 3D nesting tool gives us an exclusion box, and we've come to find that we waste too much time trying to accurately recognize what needs to be CNC'd. A rule could be made to read our iProps that are being populated anyways.

 

-> I'd like to figure out the first method first, but the view reps might work too.

 

Thanks!

 

 

0 Likes
Message 4 of 14

MechMachineMan
Advisor
Advisor

Here is how I would set it up.

 

If you wrap everything in a class and assign a shared variable, you can use the shared var in multiple subs without having to pass it/share info.

 

Class TempToShareVars
     Shared oNewAsm As Document

     Sub Main() 
         '  "Sub Main()" is what's ran first when you run the rule.
         'You want to assign oNewAsm doc here, but NOT redeclare it
         'ie oNewAsm = ThisApplication.Documents.Add(...)
    End Sub

   Sub TraverseHere()
'Check iProps here
'If iProp has cnc
'oNewAsm.Occurrences.Add( oAsmYouTraversedTo)
End Sub End Sub End Class

 

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 5 of 14

Anonymous
Not applicable

Awesome, have a good weekend. I'll let you know how it goes Monday. Thanks!

0 Likes
Message 6 of 14

Owner2229
Advisor
Advisor
Accepted solution

Hey, here's my bit to this topic:

 

 

Sub Main()
     Dim oDoc As Document = ThisApplication.ActiveDocument
     If oDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then Exit Sub
     NewAsm = ThisApplication.Documents.Open("TemplatePath", False)
     SetUpMatrix()
     SearchAssy(oDoc)
NewAsm.SaveAs("NewAssyLocation", False)
NewAsm.Close() End Sub Private NewAsm As Document Private oMatrix As Matrix Private Sub SearchAssy(oDoc As AssemblyDocument) Dim oOcs As ComponentOccurrences = oDoc.ComponentDefinition.Occurrences If oOcs.Count = 0 Then Exit Sub For Each oOcc As ComponentOccurrence In oOcs
If CheckiPro(oOcc) Then AddDoc(oOcc.Definition.Document) If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then SearchAssy(oOcc.Definition.Document) End If Next End Sub Private Function CheckiPro(oOcc As ComponentOccurrence) As Boolean Dim R As Boolean = False Try Dim oCNC As String = oOcc.Definition.Document.PropertySets.Item("Inventor User Defined Properties").Item("CNC").Expression If oCNC = "CNC" Then R = True Catch End Try Return R End Function Private Sub SetUpMatrix() Dim oTG As TransientGeometry = ThisApplication.TransientGeometry oMatrix = oTG.CreateMatrix Call oMatrix.SetToRotation(3.14159265358979 / 4, oTG.CreateVector(0, 0, 1), oTG.CreatePoint(0, 0, 0)) Call oMatrix.SetTranslation(oTG.CreateVector(1, 2, 1), True) End Sub Private Sub AddDoc(oDoc As Document) Dim FName As String = oDoc.FullFileName NewAsm.ComponentDefinition.Occurrences.Add(FName, oMatrix) End Sub

 

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 7 of 14

Anonymous
Not applicable

Hello,

 

Would you mind explaining the purpose of the matrix? Or rather how the function works? I assume this is where the added component would be placed. Does this work the same way as when shift-selecting multiple components to add to an assembly (where the parts are inserted cascaded)?

 

0 Likes
Message 8 of 14

Owner2229
Advisor
Advisor

The Matrix defines the position and rotation of the placed part.

 

This rule, as I posted it, won't add the parts cascaded, but rather just throw them all at one big pile in the new assembly (it can be changed if you wish).

For every single occurrence it finds (with the CNC iProperty), it adds one occurrence to the new assembly.

It could also be changed to not place them on one big pile, but one next to each other, or somewhere around.

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 9 of 14

MechMachineMan
Advisor
Advisor

You can see for yourself with this!

 

SyntaxEditor Code Snippet

Sub Main()    
    Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
    
    Dim oMatrix As Matrix
    oMatrix = oTG.CreateMatrix
    
    Call oMatrix.SetToRotation(3.14159265358979 / 4, oTG.CreateVector(0, 0, 1), oTG.CreatePoint(0, 0, 0))
    
    Call PrintMatrix(oMatrix)
    
    Call oMatrix.SetTranslation(oTG.CreateVector(1, 2, 1), True)
    
    Call PrintMatrix(oMatrix)
    
    Call oMatrix.SetTranslation(oTG.CreateVector(1, 2, 1), True)
    
    Call PrintMatrix(oMatrix)
    
End Sub

Sub PrintMatrix(oMatrix As Object)
    With oMatrix
        oStr = .Cell(1,1) & "   " & .Cell(1,2) & "   " & .cell(1,3) & "   " & .Cell(1,4) & vbLf & _
               .Cell(2,1) & "   " & .Cell(2,2) & "   " & .cell(2,3) & "   " & .Cell(2,4) & vbLf & _
               .Cell(3,1) & "   " & .Cell(3,2) & "   " & .cell(3,3) & "   " & .Cell(3,4) & vbLf & _
               .Cell(4,1) & "   " & .Cell(4,2) & "   " & .cell(4,3) & "   " & .Cell(4,4)
    End With
    
    MsgBox(oStr)
End Sub 

 

 

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 10 of 14

MechMachineMan
Advisor
Advisor

Hi Mike,

 

I see in this chunk of code you posted, you are using global variables. Every time I had tried to do it without wrapping it in a class before, it failed. But I'm assuming so long as you put the variable declarations after Sub Main() it works fine?

 

Have you noticed any other caveats to doing this, or is the functionality the same as putting the variable at the top of a module within vba?

 

Thanks in advance,

 

 


@Owner2229 wrote:

Hey, here's my bit to this topic:

 

 

Sub Main()
     Dim oDoc As Document = ThisApplication.ActiveDocument
     If oDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then Exit Sub
     NewAsm = ThisApplication.Documents.Open("TemplatePath", False)
     SetUpMatrix()
     SearchAssy(oDoc)
NewAsm.SaveAs("NewAssyLocation", False)
NewAsm.Close() End Sub Private NewAsm As Document Private oMatrix As Matrix Private Sub SearchAssy(oDoc As AssemblyDocument) Dim oOcs As ComponentOccurrences = oDoc.ComponentDefinition.Occurrences If oOcs.Count = 0 Then Exit Sub For Each oOcc As ComponentOccurrence In oOcs
If CheckiPro(oOcc) Then AddDoc(oOcc.Definition.Document) If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then SearchAssy(oOcc.Definition.Document) End If Next End Sub Private Function CheckiPro(oOcc As ComponentOccurrence) As Boolean Dim R As Boolean = False Try Dim oCNC As String = oOcc.Definition.Document.PropertySets.Item("Inventor User Defined Properties").Item("CNC").Expression If oCNC = "CNC" Then R = True Catch End Try Return R End Function Private Sub SetUpMatrix() Dim oTG As TransientGeometry = ThisApplication.TransientGeometry oMatrix = oTG.CreateMatrix Call oMatrix.SetToRotation(3.14159265358979 / 4, oTG.CreateVector(0, 0, 1), oTG.CreatePoint(0, 0, 0)) Call oMatrix.SetTranslation(oTG.CreateVector(1, 2, 1), True) End Sub Private Sub AddDoc(oDoc As Document) Dim FName As String = oDoc.FullFileName NewAsm.ComponentDefinition.Occurrences.Add(FName, oMatrix) End Sub

 

 


 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 11 of 14

Owner2229
Advisor
Advisor

Hey Justin, it's exactly as you wrote. As long as it is after the Sub Main, it'll work. It's just iLogic speciality...

Also the functionality is the same as in VBA, aka "Public" or "Shared" is shareable among modules/classes, while "Private" isn't (it's still shared, but only among the class/module it is inside of).

So just in case someone ends up using my samples in a bigger code with something else, I want the variables to be safe to use and not cause any errors, thus "Private".

Also, if you don't want the variable to be somehow, somewhere to be changed (it might happen by a mistake in the code and you won't even notice, as it doesn't break the functionality much otherwise), it's a good practise to use Constant, e.g.:

 

Private Const Diameter As Double = 0.25

 

I hope it makes sense as I've described it.

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 12 of 14

MechMachineMan
Advisor
Advisor

Out of curiosity, had you come across this through some Autodesk resource, or was it more of a trial & error discovery? 

 

Wondering if I'm maybe missing a resource or 2 in my collection..


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 13 of 14

Owner2229
Advisor
Advisor

I've read it somewhere, but I can't remember whether it was an tutorial or just something here on forum.

 

Anyway, here is some nice reading for long winter nights. You might know (some of) them.

knowledge.autodesk.com/support/inventor-products/learn-explore/...

http://inventortrenches.blogspot.nl/p/inventor-tutorials.html

http://modthemachine.typepad.com/my_weblog/ilogic/

 

Also I would suggest you looking into AddIns, if you haven't yet. It'll give you more options and control over Inventor:

http://modthemachine.typepad.com/files/VBAtoAddIn.pdf

http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=17324828

 

Here is a pre-made addin, all you need to do is set up your Visual Studio (or other) and build it (compile):

https://forums.autodesk.com/t5/inventor-customization/sample-code-for-inventor-add-in-dll-for-vb-net...

 

Where to place it:

https://forums.autodesk.com/t5/inventor-customization/where-to-place-inventor-addin/td-p/3314019

 

How to debug:

http://m.arch-pub.com/Debug-Inventor-Addin-Inventor-2015-Visual-Studio-Enterprise-2015_10780034.html

 

Other useful resources for AddIns:

http://spiderinnet2.typepad.com/blog/

http://spiderinnet2.typepad.com/blog/2012/07/inventor-net-find-all-the-control-definitions-1.html

http://spiderinnet2.typepad.com/blog/2012/06/inventor-net-find-all-ribbon-tab-names.html

http://spiderinnet2.typepad.com/blog/2012/06/inventor-net-find-all-ribbon-commandbar-names.html

 

 

Also, sorry for spaming on your post gfunnybus

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 14 of 14

Anonymous
Not applicable

No problem, this is all good information to me!

0 Likes