Create as new part from an occurance

Create as new part from an occurance

JorisSteurs1246
Advocate Advocate
769 Views
7 Replies
Message 1 of 8

Create as new part from an occurance

JorisSteurs1246
Advocate
Advocate

My final goal  is to replace all leaf occurrences with a copy that gets the filename as the Part Number of this occurance.

I'm not sure if I have to make new parts first and then replace them , or that I can do an equivalent in iLogic to the " save and replace component" command.

 

Anyhow this is the code I have so far.

I 'm producing the correct filenames already but it fails to do a "save as" or a "replace"

 

 

SyntaxEditor Code Snippet

fmgr = ThisApplication.Filemanager
For Each file In fmgr.files
doc = ThisApplication.Documents.ItemByName (file.fullfileName)
compdef = doc.ComponentDefinition
Dim newname As String
If (doc.documenttype = 12291) Then ' only do this for assembly or subassembly
 For Each occ In compdef.Occurrences
    If (occ.definition.type =83886592) Then ' only do this for part
        Try
               occ.Name =  iProperties.Value(occ.Name,"Project", "Part Number")
              newname = iProperties.Value(occ.Name,"Project", "Part Number") & ".ipt"
               MessageBox.Show("I found variable: " & newname , "Info")
              'occ.Replace( newname , True)
                occ.SaveAs (newname , True)
           Catch
        MessageBox.Show("error")
        End Try
    End If
 Next occ
End If
Next file
0 Likes
770 Views
7 Replies
Replies (7)
Message 2 of 8

Jef_E
Collaborator
Collaborator

Maybe this post has the answer you are looking for?

 

http://forums.autodesk.com/t5/inventor-customization/vba-save-and-replace-all-instances-of-a-part/td...



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes
Message 3 of 8

JorisSteurs1246
Advocate
Advocate

this sample uses the same line as I tried

 

SyntaxEditor Code Snippet

opartdoc.SaveAs(NewFilePath, True)

in that sample it is not inside a loop

in my loop that line gives an error. 

 

Joris.

0 Likes
Message 4 of 8

Jef_E
Collaborator
Collaborator

What is the error message? Can you take a screenshot of the messagebox where the newname is shown?



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes
Message 5 of 8

JorisSteurs1246
Advocate
Advocate

with the Try and Catch in my code  it jumped to my own error message.

When I  leave the Try and Catch out to trigger inventor's error, I get 

when trying  : occ.Replace( newname , True)
gives me  error 1 and 2  ( attached)

 

 

when trying  : occ.SaveAs (newname , True)

gives error 3 and 4  ( attached) 

 

(will have to make another post or the fourth  attachment)

0 Likes
Message 6 of 8

JorisSteurs1246
Advocate
Advocate

here is attachment 4  (looks as only 3 attachments were allowed) 

0 Likes
Message 7 of 8

Jef_E
Collaborator
Collaborator

You can't saveas a occurrence, you can only save a document

 

 

' Save-As the document
Dim oNewDoc As Document
oNewDoc = occ.Definiton.document.SaveAs(newname, True)

' Replace the occurrence with the new document
occ.Replace(oNewDoc.FullFileName, True)

 

But, I was thinking about this process you describe and why would you want to do this? Why would you want to save and replace all occurrences? 

 



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes
Message 8 of 8

JorisSteurs1246
Advocate
Advocate

Hi Jef

 

Thanks for looking into this, 

 

I've cut away some of the other code to get to the root of the problem.

I tried to do a Save As  with your suggestion,

But the code below still gives me the error as attached.

 

the reason for this code:

Before releasing an Assembly , I set all the part numbers for every part with a logical order and I want to have all the file names the same as the part numbers.

In order to have some consistency in the numbering I can do that only at the end of the project, and it is quite a bit of work.

Sometimes a new version with new partnumbers have to be made and then it starts all over again.. so basicaly wasting a good amount of time.

I thought this would be a perfect subject to  submit to some automation.

 

 

 

SyntaxEditor Code Snippet

  'check this file is an assembly
        Dim doc As Document = ThisApplication.ActiveDocument
        If doc.DocumentType = kPartDocumentObject Then
            MessageBox.Show("This rule can only be run in an assembly file!", "Stopped")
            Return
        End If

Dim newname As String
compdef = doc.ComponentDefinition
If (doc.documenttype = 12291) Then ' only do this for assembly or subassembly
 For Each occ In compdef.Occurrences
    If (occ.definition.type =83886592) Then ' only do this for part
         Dim oNewDoc As Document
            occ.Name =  iProperties.Value(occ.Name,"Project", "Part Number")
             newname = iProperties.Value(occ.Name,"Project", "Part Number") & ".ipt"
               oNewDoc = occ.Definiton.Document.SaveAs(newname, True)    
    End If
 Next 
End If

 

0 Likes