ILOGIC Save Assembly, Copy & Replace Specified Parts

ILOGIC Save Assembly, Copy & Replace Specified Parts

waldie.jonathan
Enthusiast Enthusiast
4,270 Views
21 Replies
Message 1 of 22

ILOGIC Save Assembly, Copy & Replace Specified Parts

waldie.jonathan
Enthusiast
Enthusiast

Hi Everyone,

I've been splicing some code together in order to create a rule that will 'Save As' an assembly file as well as copy and replace specific components. The problem I am having is figuring out how to specify the parts I want it to copy. There are parts as well as hardware in the assembly and I only want it to copy and replace the part files. Right now I am using "refDoc.DisplayName.EndsWith" to reference a part, but I need to reference multiple parts with different names. I assume I would use some sort of loop. I am new to and self taught in working with code so any help would be appreciated!

 

SyntaxEditor Code Snippet

InvDoc = ThisDoc.Document
 Dim refDocs As DocumentsEnumerator = InvDoc.AllReferencedDocuments
 Dim refDoc As Document
 Dim mypath As String
 mypath = InputBox("Specify Path", "Enter Desired Path", "") + "\"
 
 
 
 For Each refDoc in refDocs
 MessageBox.Show(refDoc.DisplayName)

If refDoc.DisplayName.EndsWith ("-A")= True Then

component. Dim newpartname As String newpartname = refDoc.DisplayName + InputBox("Specify Quote Extension", "Enter Desired Part Extension", "") Dim NewFileName As String 'input box to enter new file name NewFileName = newpartname 'NewFileName = InputBox("What is the new file name for " & refDoc.DisplayName & "?", "New File Name",refDoc.DisplayName) 'if the new file name is blank then use the same name If NewFileName = "" Then NewFileName = refDoc.DisplayName End If 'if the user also types in the extesion then remove the extension. If NewFileName.EndsWith(".ipt") = True Then NewFileName = NewFileName.substring(0,NewFileName.length-4) 'MessageBox.show(NewFileName) End If 'if there is a change in the name then do the following. If refDoc.DisplayName <> NewFileName & ".ipt" Then 'check to see the file already exist. if it does then replace. If System.IO.File.Exists(mypath & NewFileName & ".ipt") Then Component.Replace(refDoc.DisplayName, mypath & NewFileName & ".ipt", True) Else Dim oldName As String = refDoc.DisplayName 'if it does not exist then recreate the file refDoc.saveas(mypath & NewFileName & ".ipt",False) ' Call UpdateDrawing(Path, oldName, NewFileName) End If FileChange = True End If End If Next If FileChange = True Then NewAssemblyName = InputBox("What is the new file name for the assembly?", "New Assembly File Name",iProperties.Value("Project", "Part Number")) If NewAssemblyName <> "" Then Dim oldName As String = ThisDoc.FileName(True) 'include extension ThisDoc.Document.SaveAs(mypath & NewAssemblyName & ".iam" , False) ' Call UpdateDrawing(Path, oldName, NewAssemblyName) End If End If iLogicVb.UpdateWhenDone = True
4,271 Views
21 Replies
Replies (21)
Message 2 of 22

xiaodong_liang
Autodesk Support
Autodesk Support
Hi,

is 'hardware' also a kind of part? or pipe? frame? or others? If you could provide a small demo, we can take a look whether it has some unique identifier that can differentiate to a common part.
0 Likes
Message 3 of 22

waldie.jonathan
Enthusiast
Enthusiast

Hi Xiaodong,

Thanks for your reply.

 

The hardware parts are nuts & bolts, and are identified with a 5 digit part number (ie. 71291, 40303, 31869 etc). There is one other part that I don't want it to copy and replace which is named 'BOC-H'.

 

Part numbers to be copied and replaced are typically named in similar in format to the following: LT-1, FPB-1, FPB-1_MIR. 

Not sure if it would be easier to have it copy all parts except those with a 5 digit part number, and the specified 'BOC-H'...?

 

Let me know if you would like any further information or clarification.

 

Any assistance is appreciated.

 

 

0 Likes
Message 4 of 22

waldie.jonathan
Enthusiast
Enthusiast

I have attached a copy of the BOM defining which parts are to be copied and replaced, and which parts/hardware are to be excluded. 

0 Likes
Message 5 of 22

MechMachineMan
Advisor
Advisor

You might want to look at a Select Case code. ie;

 

Select oPartNumber

 Case 91230

    'do nothing

 Case 912312

    'do nothing

Case Boc-H

 'do nothing

Case else

 ' run save and replace code

End Select

 

or something to that effect to make a list of files to skip. You can also write code that will look to see if the string is either convertable to an integer (CInt function) or if it equals "BOC-H". Also, a string of if statements could be used as well, but it would get messy.

 

Another option, if they are all standard parts is to check if the Part Document.IsModifiable and run code based off of that.

 

- MechMachineMan


--------------------------------------
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 6 of 22

xiaodong_liang
Autodesk Support
Autodesk Support

Hi,

 

I meant the demo model file, instead of the name list. If with name list only, what I can suggest is same to that MechMachineMan mentioned in message 5. 

 

however probably your 'hardware' part is a kind of the special parts such as  Cable and Harness, Tube and Piping ,Frame Generator and Design Accelator. API provides the way to determine the type of document DocumentInterests

 

'Cable & Harness routing - {C6107C9D-C53F-4323-8768-F65F857F9F5A}
'Tube & Pipe routing - {4D39D5F1-0985-4783-AA5A-FC16C288418C}
'Autodesk Frame Generator Add-in - {AC211AE0-A7A5-4589-916D-81C529DA6D17}
'Design Accelerator Add-in - {BB8FE430-83BF-418D-8DF9-9B323D3DB9B9}

 

Sub DocHasInteres() 
	Dim odocints As DocumentInterests 
	Set odocints = ThisApplication.ActiveDocument.DocumentInterests 
	Dim hasinterest As Boolean 
	'Check for Tube and Pipe routing 
	hasinterest = odocints.hasinterest("{4D39D5F1-0985-4783-AA5A-FC16C288418C}")
	If hasinterest Then 
	Dim odocint As DocumentInterest 
	For Each odocint In odocints 
		'dump the info
		Debug.Print odocint.Name 
		Next 
	End If 
End Sub 

 

If your 'hardware' is none of the type above, you have to check the name. Or store one attribute with the document in advance, and check the attribute and filter out the parts you are concerned. 

Message 7 of 22

waldie.jonathan
Enthusiast
Enthusiast

I have attached a simplified 'demo' assembly of some of the components and hardware I use in the assembly. Included is an updated excel list differentiating between stock parts/hardware and custom parts to be copied/replaced.

Let me know what you think. I like the idea of storing an attribute in the custom components and having the rule search for those, unless there is an easier or cleaner way of doing it.

 

Thanks MechMachineMan, I like your suggestion, but feel in this application it may be somewhat cumbersome. I will keep it in mind though.

 

 

 

0 Likes
Message 8 of 22

xiaodong_liang
Autodesk Support
Autodesk Support

It looks those parts are common parts while the others are sheet metal. If it is always the behavior, you could simply use Document.subtype to check whether it is a sheet metal or not:

 

If DocumentObject.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
   ' sheet metal file

   ' continue the workflow of replace.

Else

    ' other type files

    ' ignore

End If

0 Likes
Message 9 of 22

Anonymous
Not applicable

Where I work we set nuts/bolts etc as purchased parts in the bill of materials. Then you know that only the "normal" parts need replacing. 

 

 

0 Likes
Message 10 of 22

waldie.jonathan
Enthusiast
Enthusiast

Hi Xiaodong,

 

I tried putting your suggestion in but not sure what im missing. I appologize, im pretty new to this so it is likely something obvious that im missing.

 

I am getting the error "Public member 'DocumentObject' on type 'PartDocument' not found."

 

SyntaxEditor Code Snippet

InvDoc = ThisDoc.Document
Dim refDocs As DocumentsEnumerator = InvDoc.AllReferencedDocuments
Dim refDoc As Document
Dim mypath As String
mypath = InputBox("Specify Path", "Enter Desired Path", "") + "\"
 
Dim DocumentObject As Document
 
For Each refDoc in refDocs
'MessageBox.Show(refDoc.DisplayName)

If refdoc.DocumentObject.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then 
   ' sheet metal file
   ' continue the workflow of replace.
   
 Dim newpartname As String
 newpartname = refDoc.DisplayName + InputBox("Specify Quote Extension", "Enter Desired Part Extension", "")
 
 Dim NewFileName As String
 'input box to enter new file name
 NewFileName = newpartname
 'NewFileName = InputBox("What is the new file name for " & refDoc.DisplayName & "?", "New File Name",refDoc.DisplayName)
   
     'if the new file name is blank then use the same name
     If NewFileName = "" Then
         NewFileName = refDoc.DisplayName
     End If
   
     'if the user also types in the extesion then remove the extension.
     If NewFileName.EndsWith(".ipt") = True Then
         NewFileName = NewFileName.substring(0,NewFileName.length-4)
         'MessageBox.show(NewFileName)
     End If
   
     'if there is a change in the name then do the following.
     If refDoc.DisplayName <> NewFileName & ".ipt" Then
         'check to see the file already exist. if it does then replace.
         If System.IO.File.Exists(mypath & NewFileName & ".ipt") Then
             Component.Replace(refDoc.DisplayName, mypath & NewFileName & ".ipt", True)
         Else
             Dim oldName As String = refDoc.DisplayName
             'if it does not exist then recreate the file
             refDoc.saveas(mypath & NewFileName & ".ipt",False)
            ' Call UpdateDrawing(Path, oldName, NewFileName)
         End If
       
         FileChange = True
     End If
   End If
 Next
 
 If FileChange = True Then
     NewAssemblyName = InputBox("What is the new file name for the assembly?", "New Assembly File Name",iProperties.Value("Project", "Part Number"))
   
     If NewAssemblyName <> "" Then
         Dim oldName As String = ThisDoc.FileName(True) 'include extension
         ThisDoc.Document.SaveAs(mypath & NewAssemblyName & ".iam" , False)
       '  Call UpdateDrawing(Path, oldName, NewAssemblyName)
     End If
Else
End If
 
 iLogicVb.UpdateWhenDone = True
 
0 Likes
Message 11 of 22

waldie.jonathan
Enthusiast
Enthusiast

DRLTKSE, Thanks for your suggestion.

Unfortunately our company hasn't made purchased/stock part BOM classification a priority, other than simply giving them part numbers.  

0 Likes
Message 12 of 22

MechMachineMan
Advisor
Advisor

@waldie.jonathan wrote:

Hi Xiaodong,

 

I tried putting your suggestion in but not sure what im missing. I appologize, im pretty new to this so it is likely something obvious that im missing.

 

I am getting the error "Public member 'DocumentObject' on type 'PartDocument' not found."

 

SyntaxEditor Code Snippet

Dim DocumentObject As Document
 
For Each refDoc in refDocs
'MessageBox.Show(refDoc.DisplayName)

If refdoc.DocumentObject.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then 
   ' sheet metal file
   ' continue the workflow of replace.
   
Your code doesnt make any sense. The refdoc and DocumentObject are redundant. You need to get rid of one. Using the For Each loop it goes through each DocumentObject anyways.
>DocumentObject' on type 'PartDocument' not found."
Indicates you are telling it to look for DocumentObject within a Part Document... which it cant because you can't have a document within a document aside from being occurrences within in an assembly or reference documents.


 


--------------------------------------
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 22

waldie.jonathan
Enthusiast
Enthusiast

Thanks MechMachineMan,

I took it out, but now it is giving me the error that I need to set an instance..

 

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

0 Likes
Message 14 of 22

MechMachineMan
Advisor
Advisor

Did you take the refDoc out instead of the DocumentObject?

 

 

Sorry, I could have been more clear the first time.

 

 

refDoc should stay in as that is what has an assosciation

 

 

 

For Each refDoc in refDocCollection

 

     refDoc.DocumentSubType = DocumentTypeEnumerator

 

Next


--------------------------------------
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 15 of 22

waldie.jonathan
Enthusiast
Enthusiast

Thanks, that worked!

Only problem now is that I want it to copy all but one sheet metal part with the name "BOC-H". Do you know of a way to create an exception for this one?

0 Likes
Message 16 of 22

xiaodong_liang
Autodesk Support
Autodesk Support
Hi waldie.jonathan,

DocumentObject in my code just refers to a document. Sorry that is confused you.

as to isolating 'BOC-H', if there is not any flags, you can only use name to filter. e.g. your excel sheet indicates if this is a part that needs to be filtered out. The code reads out the flag and follow the other process.
0 Likes
Message 17 of 22

P_Korgaonkar
Advocate
Advocate

Hi waldie.jonathan,

 

DId you copy assembly code work in the end with changes?

 

If yes do mind sharing it with others?

 

It would help us new learners. I am new to Ilogic and creating assemblies which needs to be copied on reglar basis, with each part number needs to change, but keep the library parts unchanged,

 

All my parts are numberd such that, all I need is Prefix of a new number, which will be new every time, and I should be able input this new number in aform.

 

Thanks in Advance.

0 Likes
Message 18 of 22

MechMachineMan
Advisor
Advisor
Have you looked into iLogi design copy?

--------------------------------------
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 19 of 22

P_Korgaonkar
Advocate
Advocate

Thanks Justin,

 

Yes currently using that. I need to select each part to be copied, and foloow a little procedure. (Not sure if these is any further easy or quicker way)

If there is, please feel free to suggest, or give me a link.

 

Wondering if it is possble to do it by Ilogic code, where all parts to be copied are pre defined.

The folder location and prefix from a form is used for re numbering, then it becomes ver easy and speedy operation for any novice cad user.

 

 

Regards

Parag

0 Likes
Message 20 of 22

Anonymous
Not applicable

I would use ilogic to check the path of the parts so as not to replace any libary parts

0 Likes