- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
iLogic external rule Inventor 2013 - Find Component Place into a New .iam
Hello everyone!!! I posted this exact question in, what I think, the wrong section of this forum.
I am trying to write a rule that will allow me to "Find Component" by "Part Number" of an assembly and place them in another assembly.
Example: I have Part 1,2,3,4 etc.... and that part might have many different features on it (Part 1.1, Part 1.2, Part 2.1, Part 2.2, Part 3.1 etc...)
I need all "Part 1's" to go in to an assembly and all "Part 2's" in separate assembly and so on, so that I can reference the new Part 1.iam for a fab table.
Any help would be greatly appreciated.
Thank you,
Dylan Stewart
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am beginning to wonder if this is even possible? Seems simple enough (for someone who has iLogic and coding experience) but I just can't seem to figure out how to access the 'Find Component' feature and I have yet to see anything on how to create a new assembly with a macro.
My thought process is this; If you can click a button to make it happen, then you should be able to automate it, correct?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
This code will partially do what you want, based upon the assumption that the browser nodes in your assembly are the same as the part number (which they usually are by default):
If you can create a simple vba userform with a textbox and button within the VBA editor, then insert this code into the button click action.
This code will look through all levels of the assembly and DELETES ALL the components it comes across that DO NOT MATCH what was typed into the textbox. So I would advise saving or creating a copy of the assembly you want to purge before doing anything further.
hopefully this will be of some use to you, or at least give you a starting point as I haven't had a huge amount of time to debug this thoroughly.
Private Sub CommandButton1_Click()
' Get the active assembly.
Dim oAsmDoc As AssemblyDocument
Set oAsmDoc = ThisApplication.ActiveDocument
' Get the assembly component definition.
Dim oAsmDef As AssemblyComponentDefinition
Set oAsmDef = oAsmDoc.ComponentDefinition
' Get all of the leaf occurrences of the assembly.
Dim oLeafOccs As ComponentOccurrencesEnumerator
Set oLeafOccs = oAsmDef.Occurrences.AllLeafOccurrences
' Iterate through the occurrences and print the name.
Dim oOcc As ComponentOccurrence
For Each oOcc In oLeafOccs
'Delete the parts that don't match
Dim oName As String
Dim strOrig As String
Dim Ref1 As String
Dim L As Integer
strOrig = oOcc.name
Ref1 = Left(strOrig, InStr(strOrig, ":"))
L = Len(Ref1) - 1
If Ref1 <> TextBox1.Text & ":" Then
oOcc.Delete
End If
Next
End Sub
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi, try this one:
Dim oDoc As Document = ThisApplication.ActiveDocument
If Not oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("This rule is for assembly only.")
Exit Sub
End If
OrigASM = ThisDoc.PathAndFileName(True)
SearchedPart = iProperties.Value("Project", "Part Number")
SearchedPart = InputBox("Please insert prefix of searched parts", "Prefix", DXFName)
If SearchedPart = vbNullString Then
Exit Sub
End If
NewName = ThisDoc.FileName(False) & " " & iProperties.Value("Project", "Description")
NewName = InputBox("Please insert new number and name for first new assembly", "New ASM name", DXFName)
If NewName = vbNullString Then
Exit Sub
End If
NewName1 = ThisDoc.FileName(False) & " " & iProperties.Value("Project", "Description")
NewName1 = InputBox("Please insert new number and name for second new assembly", "New ASM name", DXFName)
If NewName1 = vbNullString Then
Exit Sub
End If
Dim oPath As String = ThisDoc.Path
q1 = MessageBox.Show("Copy to the same location?", "Location",MessageBoxButtons.YesNo)
If q1 = vbNo Then
Dim dialog1 = New System.Windows.Forms.FolderBrowserDialog()
dialog1.SelectedPath = oPath
dialog1.ShowNewFolderButton = True
If System.Windows.Forms.DialogResult.OK = dialog1.ShowDialog() Then
oPath = dialog1.SelectedPath
Else
MsgBox("No folder selected. Copiyng canceled.")
Exit Sub
End If
End If
oPath = oPath & "\"
If oPath = vbNullString Then
Exit Sub
End If
oDoc.SaveAs(oPath & NewName & ".iam", True)
pDoc = ThisApplication.Documents.Open(oPath & NewName & ".iam")
oDoc = pDoc
Dim acd As ComponentOccurrencesEnumerator = oDoc.ComponentDefinition.Occurrences.AllLeafOccurrences
Dim Occ As ComponentOccurrence
Dim compCount As Integer = oDoc.AllReferencedDocuments.Count * 2
Dim oResult As String
Dim FNamePos As Long
Dim docFName As String
Dim oProgressBar As Inventor.ProgressBar = ThisApplication.CreateProgressBar(False, compCount, "Copiyng assembly", True)
Amount = 100 / compCount
Progress = 0
For Each Occ in acd
ModelFileName = Occ.Name
FNamePos = InStrRev(ModelFileName, "\", - 1)
docFName = Mid(ModelFileName, FNamePos + 1, Len(ModelFileName) - FNamePos)
oResult = Left(docFName, Len(SearchedPart))
If oResult <> SearchedPart Then
Occ.Delete
End If
Progress = Progress + 1
oProgressBar.Message = ("Completed: " & Round(Progress * Amount) & "/" & "100%")
oProgressBar.UpdateProgress
Next
pDoc = ThisApplication.Documents.Open(OrigASM)
oDoc = pDoc
oDoc.SaveAs(oPath & NewName1 & ".iam", False)
acd = oDoc.ComponentDefinition.Occurrences.AllLeafOccurrences
compCount = oDoc.AllReferencedDocuments.Count
For Each Occ in acd
ModelFileName = Occ.Name
FNamePos = InStrRev(ModelFileName, "\", - 1)
docFName = Mid(ModelFileName, FNamePos + 1, Len(ModelFileName) - FNamePos)
oResult = Left(docFName, Len(SearchedPart))
If oResult = SearchedPart Then
Occ.Delete
End If
Progress = Progress + 1
oProgressBar.Message = ("Completed: " & Round(Progress * Amount) & "/" & "100%")
oProgressBar.UpdateProgress
Next
oProgressBar.Close
- - - - - - - - - - - - - - -
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
This code seems to be the start of EXACTLY what I am looking for.
However, All this does at the moment is take an existing assembly and copy and paste it in a new assembly.
I would like to pick and choose which parts go in to the new assembly.
I am also getting these error messages.
{
Error in rule: Find And Place Stock Numbers In IAM, in document: BRAKE METAL.iam
Public member 'ComponenetDefinition' on type '_DocumentClass' not found.
}
More Info
{
System.MissingMemberException: Public member 'ComponenetDefinition' on type '_DocumentClass' not found.
at Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack)
at LmiRuleScript.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)
}
Could I possibly get some comments on the code to show me exactly what it is doing?
Again,
Thank you so much for your help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm still trying to get this one to work.
It says that "Let" and "Set" is no longer supported.
I am working with Inventor 2013
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi DStewartRG3E7,
Concerning the "Set" issue, see this link:
http://adndevblog.typepad.com/manufacturing/2015/11/convert-vba-to-net-ilogic.html
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi, do you have some other code in your rule besides what I posted above? Because this rule works fine for me and without any errors.
You can try to recopy the rule again, because it says it can't find 'ComponenetDefinition' but in the rule it should use 'ComponentDefinition'. You have there one extra 'E'.
You wanted to copy parts picked by theyr prefix, didn't you? That's what this rule should do...
First it asks for prefix ("Please insert prefix of searched parts"),
then for the names of first ("Please insert new number and name for first new assembly")
and second ("Please insert new number and name for second new assembly") assembly
and finaly for the location of new assemblies ("Copy to the same location?").
I was testing it, so it should work.
E.g.: You have folowing parts:
001A
001B
002A
002B
003A
You want to make new assembly with all parts '001...', so you write '001' when it asks for prefix
and it will create one new assembly with parts 001A and 001B,
and second assembly with parts 002A, 002B and 003A.
- - - - - - - - - - - - - - -
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Perhaps I was not very clear in exactly what I was looking for, and for that I apologize.
Here is what a typical BOM will look like for me.
| MARK NO. | QTY | LENGTH | Stock Number | Part Number |
---------------------------------------------------------------------------------------
| M1001 | 4 | 100" | RUCW3003 | Metal Bracket |
| M1002 | 3 | 60" | RUCW3003 | Metal Bracket |
| M1003 | 8 | 75" | RUCW3003 | Metal Bracket |
| C1001 | 10 | 12" | RUCW4040 | Channel |
| C1002 | 25 | 10" | RUCW4040 | Channel |
| G1001 | 15 | 35" | UCW6062 | Gasket |
| G1002 | 15 | 30" | UCW6062 | Gasket |
And so on and so on.....
So what I would like to have happen is find ALL RUCW (from a list or ?library?) to add in to the program.
So instead of using the Find Component feature on Inventor 2013 and navigating in 'SEARCH FOR' - Stock Number and typing RUCW3003, RUCW5689, RUCW9742 etc etc......
-then waiting on the program to find all of those and high light them so that I can right click and copy and open up a new iam and then right click paste....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
You can use this to add ALL parts from one directory selected by prefix to your currently opened assembly:
Imports System.IO
Imports System.IO.File
Sub Main
Dim oDoc As Document = ThisApplication.ActiveDocument
If Not oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("This rule is for assembly only.")
Exit Sub
End If
'You can change this line to match your location if you have all files in one folder
'Otherwise it will be used as initial folder for folder search
oPath = "C:\FolderContainingAllParts"
'Or you can use this to select the folder, delete it if not needed
Dim dialog1 = New System.Windows.Forms.FolderBrowserDialog()
dialog1.SelectedPath = oPath
dialog1.ShowNewFolderButton = True
If System.Windows.Forms.DialogResult.OK = dialog1.ShowDialog() Then
oPath = dialog1.SelectedPath
Else
MsgBox("No folder selected. Copiyng canceled.")
Exit Sub
End If
If Not System.IO.Directory.Exists(oPath) Then
MsgBox("Folder doesn't exist")
End If
SearchedPart = InputBox("Please insert prefix of searched parts", "Prefix", DXFName)
If SearchedPart = vbNullString Then
Exit Sub
End If
Dim oFile As String
'It will only look for parts, as there is "*.ipt" at the end
For Each oFile In System.IO.Directory.EnumerateFiles(oPath, SearchedPart & "*.ipt")
AddOccurrence(oFile)
Next
End Sub
Public Sub AddOccurrence(oPartToAdd As String)
Dim oDoc As Document = ThisApplication.ActiveDocument
Dim oAsmCompDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oMatrix As Matrix = oTG.CreateMatrix
Call oMatrix.SetToRotation(3.14159265358979 / 4, _
oTG.CreateVector(0, 0, 1), oTG.CreatePoint(0, 0, 0))
Call oMatrix.SetTranslation(oTG.CreateVector(3, 2, 1))
Dim oOcc As ComponentOccurrence
oOcc = oAsmCompDef.Occurrences.Add(oPartToAdd, oMatrix)
End Sub
- - - - - - - - - - - - - - -
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Could you possibly attach some examples of what you are using?
I feel like this should work for me, but not sure if I am explainging myself clear enough.
Thank you so much for your help!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi, here is an example:
I have these files in the folder from which I want to load all files which start with "PC-0246":
So when I start the rule from new, empty assembly, I first select the folder in which I have these parts and then I write in "PC-0246":
And now I have all these parts in the assembly, just like I wanted:
If you are looking for parts by iProperties then we can alter the rule to look at them instead.
- - - - - - - - - - - - - - -
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I think I am starting to understand why this isn't working for me.
In my model browser, none of my parts say .ipt
Yes, I am trying to search for 'Stock Number' through the iProperties.
Do I have to have a blank assembly already open? I was hoping to maybe have my massive assembly open and be able to type in as follows...
RUCW1234, RUCW9999, RUCW9876, RUCW5555 and so on...... (There are TONS of these numbers...)
Now that I have all the 'Stock Numbers' picked out that I want in my new assembly from my old assembly, I would like the option to save the new assembly as a name of my choosing.
Once I have this part of the rule done, there are many more steps I would like to take.
I really appreciate your help!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi, I believe we can adjust the rule to your need, I just need some info:
1) What is the extension of your parts (or extensions, if you have more than one).
2) Can you add all parts starting with "RUCW" or you need to type in the whole number?
3) Do you want to add subassemblies or parts only?
4) Can you use manual "Save As" to save the assembly or you want it to automaticaly pop up the "Save As" dialog at the end of the rule?
5) Do you want to add parts from pre-specifed folder or you want to choose the folder on each run of the rule or you want to pick the parts from some kind of part list (Excel table, TXT, etc.) or from another assembly?
You don't need to have open blank assembly, any assembly would work.
- - - - - - - - - - - - - - -
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
We name our assemblies after the main part in that assembly.
All of our sub-assemblies are made from the frame generator.
So once we have everything in the main assembly, we need to sort through all of our parts so that we can then assign mark #'s to these parts.
(This one just happens to be complete already)
I don't know if it would be too much to ask out of a program to place all the RUCW (of my choosing by entering data in when prompted) in to different assemblies.
Example.
-Run Rule- "Would you like to search this assembly" (Already open assembly)
-Click yes/no- If yes Then - "What STOCK NUMBERS would you like to search for?"
-Enter RUCW123, RUCW4321, RUCW9876, UCW1442 and so on.-
- Program Searches for all of the user defined STOCK NUMBERS and automatically places them in to a new assembly.
(For instance, the program finds 14 different parts with STOCK NUMBER of RUCW1234 and knows to place them all together, finds 10 RUCW4321 and know to place them in a completely different assembly and so on for however many different STOCK NUMBERS the user has defined.)
So to answer your questions.
1.) I have attached some sample pictures on some projects I have been working on.
2.) It would be preferable to pick and choose which ones go where.
3.) Parts only please.
4.) We can absolutely use a manual Save As.
5.) Would like to search the OPEN assembly using either a excel file with a list of STOCK NUMBERS
OR, would like to be able to tell the program what I want to find. (ie.. Program - "What do you want to find"
User - :type: - RUCW1234 (if user wants to search for more than one
then separate with a comma)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi, this rule below should meet your needs.
1) It will ask for Stock Numbers. Write in e.g.: RUCW001;RUCW002;RUCW003
(I can change it to use the Excel table or text file if you want)
2) Then it will create new assembly for each specifed Stock Number. But it won't save it (I can change it for you, if you want).
3) And then it will look for parts with matching Stock number and add them to these assemblies.
Sub Main()
Dim g_App As Inventor.InventorServer = ThisApplication
Dim oDoc As Document = ThisApplication.ActiveDocument
If Not oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("This rule is for assembly only.")
Exit Sub
End If
Dim OrigASM As String = ThisDoc.PathAndFileName(True)
Dim oPath As String = ThisDoc.Path & "\"
'SearchedPartExample = "RUCW001;RUCW002;RUCW003"
SearchedPart = InputBox("Please insert stock number of searched parts", "Input")
If SearchedPart = vbNullString Then
Exit Sub
End If
'specify word splitting character ";"
Dim Separators() As Char = {";"c}
oStockNumbers = SearchedPart.Split(Separators)
i = 0
For Each oStockNR In oStockNumbers
Dim oNewAsmDoc(i) As AssemblyDocument
oNewAsmDoc(i) = g_App.Documents.Add(DocumentTypeEnum.kAssemblyDocumentObject, , True)
Call LookForStockNR(oDoc, oStockNumbers(i), oNewAsmDoc(i))
i += 1
Next
oDoc.Activate
End Sub
Sub LookForStockNR(oDoc As Document, oStockNR As String, oNewDoc As AssemblyDocument)
Dim refDocs As DocumentsEnumerator = oDoc.AllReferencedDocuments
Dim refDoc As Document
For Each refDoc in refDocs
ModelFileName = refDoc.FullFileName
FNamePos = InStrRev(ModelFileName, "\", - 1)
docFName = Mid(ModelFileName, FNamePos + 1, Len(ModelFileName) - FNamePos)
If iProperties.Value(docFName, "Project", "Stock Number") = oStockNR Then
Call AddOccurrence(oNewDoc, ModelFileName)
End If
Next
End Sub
Sub AddOccurrence(oNewDoc As AssemblyDocument, oPartToAdd As String)
Dim oAsmCompDef As AssemblyComponentDefinition = oNewDoc.ComponentDefinition
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oMatrix As Matrix = oTG.CreateMatrix
Call oMatrix.SetToRotation(3.14159265358979 / 4, _
oTG.CreateVector(0, 0, 1), oTG.CreatePoint(0, 0, 0))
Call oMatrix.SetTranslation(oTG.CreateVector(3, 2, 1))
Dim oOcc As ComponentOccurrence
oOcc = oAsmCompDef.Occurrences.Add(oPartToAdd, oMatrix)
End Sub
- - - - - - - - - - - - - - -
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
So I tried this code this morning, and it doesn't seem to like this line:
oNewAsmDoc(i) = g_App.Documents.Add(DocumentTypeEnum.kAssemblyDocumentObject, , True)
These are the error messages I'm getting:
'kAssemblyDocu' is not a member of 'Inventor.DocumentTypeEnum'.
Comma,')', or a valid expression continuation expected.
Thank you,
Dylan Stewart
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi, I don't know where the problem is, as the rule is runing correctly for me. The line seems to be fine.
Try to copy it from the attached text file instead.
- - - - - - - - - - - - - - -
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
AWESOME!!!!! It's working!!
Only a couple of problems that I am having now. One is that it is only bringing in one instance on each, where as my assembly might sometimes have +- 25...
It also missed some of the parts. (I had everything merged by Part Number - Not a big deal as I ran it when I took that feature off)
Now this one seemed kind of weird to me...
Only with 1 of my RUCW did this happen:
Worked perfectly
Worked perfectly
Only brought in one part...?
Well, so I thought... The other RUCW8944 parts ended up in the RUCW8955 .iam ??
Now back to normal, working perfectly.
Thank you so much for your help!!!
I would like to brain storm with you on some other projects I have once we get this one all debugged.
You have been loads of help!!!