rename or saveas?

rename or saveas?

martinhoos
Advocate Advocate
1,476 Views
12 Replies
Message 1 of 13

rename or saveas?

martinhoos
Advocate
Advocate

Hello Forum,

i have a IAM (master.iam) and a IDW (master.idw), both with scripts, that means i can change the IAM with iLogic after this i save the IAM and open the IDW with automatic ilogic (runs after open). My wish is, to rename or saveas the IAM and the IDW via iLogic. I dont know how, pls can you help?

Regards

Martin

 

 

0 Likes
Accepted solutions (2)
1,477 Views
12 Replies
Replies (12)
Message 2 of 13

Owner2229
Advisor
Advisor

Hey, do you want to "save as" only the top assembly file or also all sub-assemblies and sub-parts?

Here's the simple code (without any inputs, error catching, exceptions etc.).

It will re-save ONLY the top file.

 

Dim oDoc As Document = ThisApplication.ActiveDocument
Dim oNewName As String = "C:\NewPath\NewName"
Dim Ext As String = vbNullString
Select Case oDoc.DocumentType
Case DocumentTypeEnum.kPartDocumentObject:      Ext = ".ipt"
Case DocumentTypeEnum.kAssemblyDocumentObject:  Ext = ".iam"
Case DocumentTypeEnum.kDrawingDocumentObject:   Ext = ".idw"
Case Else: Exit Sub
End Select
oNewName = oNewName & Ext
oDoc.SaveAs(oNewName, True)
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 3 of 13

martinhoos
Advocate
Advocate

Hi Mike,

thanks a lot...

i like to rename only the top assembly and the top drawing.

With your code the new idw is still looking for the old assembly (master.iam). Is it posible to "save as" or "rename" the idw and automaticly the iam? Hard to explain ...

Regards Martin

0 Likes
Message 4 of 13

Owner2229
Advisor
Advisor

I know what you mean. Does the IDW and IAM by any chance have the same name? Like:

MyAwesomeAssembly.iam

MyAwesomeAssembly.idw

 

Also, would you like to run the rule from the assembly or from the drawing?

Note:

The drawing knows exactly the location and name of the assembly.

If run from the assembly, the rule will have to guess the drawing's name and look for 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
0 Likes
Message 5 of 13

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi Martin,

 

Please try the following sample code in master.idw (DrawingDocument).

 

	Dim drawingDoc As DrawingDocument
        drawingDoc = mApp.ActiveDocument

        drawingDoc.SaveAs("NewPath\new_master.idw", False)

        Dim oSheet As Sheet
        oSheet = drawingDoc.Sheets.Item(1)

        Dim oView As DrawingView
        oView = oSheet.DrawingViews.Item(1)

        Dim assyDoc As AssemblyDocument
        assyDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument

        assyDoc.SaveAs("NewPath\new_master.iam", False)

Please feel free to contact if there is any doubt.

 

If solves your problem, click on "Accept as solution" / give a "kudo"

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 6 of 13

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi Martin,

 

Oops...Previous code was in VB.net.

 

Please find the iLogic code in the following. This code need to run in IDW(master.idw).

 

    Dim drawingDoc As DrawingDocument
    drawingDoc = ThisApplication.ActiveDocument

    drawingDoc.SaveAs("NewPath\new_master.idw", False)

    Dim oSheet As Sheet
    oSheet = drawingDoc.Sheets.Item(1)

    Dim oView As DrawingView
    oView = oSheet.DrawingViews.Item(1)

    Dim assyDoc As AssemblyDocument
    assyDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument

    assyDoc.SaveAs("NewPath\new_master.iam", False)

 

Please feel free to contact if there is any doubt.

 

If solves your problem, click on "Accept as solution" / give a "kudo"

 

Thanks and regards,

 

 

 


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 7 of 13

martinhoos
Advocate
Advocate

Hi Mike,

the way i like to do this:

 

1. copy the master.iam and the master.idw to an new Folder.

2. make changes with scipting in the iam and also in the idw.

3. rename the iam and the idw, so that the new idw is looking for the new iam

 

To answer your question, yes, both have the same Name: "master"

 

I like to run the rule from the idw.

 

Thank you very much.

Regards

Martin

0 Likes
Message 8 of 13

Owner2229
Advisor
Advisor

Here you go. You can run it from drawing, assembly or part. It should handle it all.

 

Sub Main()
	Dim oDoc As Document = ThisApplication.ActiveDocument
	Dim oFD As FileDialog
	Call ThisApplication.CreateFileDialog(oFD)
	oFD.Filter = "Inventor Files (*.idw;*.iam;*.ipt)|*.idw;*.iam;*.ipt"
	oFD.FilterIndex = 1
	oFD.DialogTitle = "Select folder and new filename for new drawing / assembly / file."
	oFD.InitialDirectory = oDoc.FullFileName
	oFD.ShowSave()
	Dim oNewName As String = oFD.FileName
	If oNewName = vbNullString Then Exit Sub
	Dim FNP As Integer = InStrRev(oNewName, ".", -1)
	oNewName = Left(oNewName, FNP)
	If oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
		FromDrawing(oDoc, oNewName)
	Else
		FromPart(oDoc, oNewName)
	End If
End Sub

Sub FromDrawing(oDoc As DrawingDocument, oNewName As String)
	Dim oSheet As Sheet = oDoc.Sheets.Item(1)
	Dim pNewName As String = vbNullString
	If oSheet.DrawingViews.Count > 0 Then
		Dim oView As DrawingView = oSheet.DrawingViews.Item(1)
		Dim pDoc As Document = oView.ReferencedDocumentDescriptor.ReferencedDocument
		pNewName = SavePart(pDoc, oNewName)
	End If
	SaveDrawing(oDoc, oNewName, pNewName)
End Sub

Sub FromPart(oDoc As DrawingDocument, oNewName As String)
	Dim OldName As String = oDoc.FullFileName
	Dim pNewName As String = SavePart(oDoc, oNewName)
	Dim FNP As Integer = InStrRev(OldName, ".", -1)
	OldName = Left(OldName, FNP) & "idw"
	Dim pDoc As DrawingDocument = ThisApplication.Documents.Open(OldName, True)
	SaveDrawing(pDoc, oNewName, pNewName)
End Sub

Function SavePart(oDoc As DrawingDocument, oNewName As String) As String
	Dim Ext As String = GetExt(oDoc)
	If Ext = vbNullString Then Return vbNullString
	Dim NewName As String = NewName & Ext
	oDoc.SaveAs(NewName, False)
	Return NewName
End Function

Sub SaveDrawing(oDoc As DrawingDocument, oNewName As String, pNewName As String)
	Dim NewName As String = NewName & "idw"
	oDoc.SaveAs(NewName, False)
	oDoc.Close(True)
	If pNewName = vbNullString Then Exit Sub
	Dim nDoc As DrawingDocument = ThisApplication.Documents.Open(NewName, True)
	Dim oRefFile As Inventor.FileDescriptor = nDoc.File.ReferencedFileDescriptors(1)
	Try
		oRefFile.ReplaceReference(pNewName)
	Catch
	End Try
	nDoc.Save()
End Sub

Function GetExt(oDoc As Document) As String
	Select Case oDoc.DocumentType
	Case DocumentTypeEnum.kPartDocumentObject:	Return "ipt"
	Case DocumentTypeEnum.kAssemblyDocumentObject:	Return "iam"
	End Select
	Return vbNullString
End Function
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 13

martinhoos
Advocate
Advocate

Hi Mike,

i get a message, may be i made a mistake.

 

I copied your code in a iLogic rule (IDW) and after it runs i get this message:

 

Unbenannt.JPG

 

 

Thanks for your help!

 

Regards

Martin

0 Likes
Message 10 of 13

Owner2229
Advisor
Advisor
Accepted solution

I had to fix a couple of typos, but now it should be working:

 

Sub Main()
	Dim oDoc As Document = ThisApplication.ActiveDocument
	Dim oFD As FileDialog
	Call ThisApplication.CreateFileDialog(oFD)
	oFD.Filter = "Inventor Files (*.idw;*.iam;*.ipt)|*.idw;*.iam;*.ipt"
	oFD.FilterIndex = 1
	oFD.DialogTitle = "Select folder and new filename for new drawing / assembly / file."
	oFD.InitialDirectory = oDoc.FullFileName
	oFD.ShowSave()
	Dim oNewName As String = oFD.FileName
	If oNewName = vbNullString Then Exit Sub
	Dim FNP As Integer = InStrRev(oNewName, ".", -1)
	If FNP > 0 And FNP > Len(oNewName) - 4 Then
		oNewName = Left(oNewName, FNP)
	Else
		oNewName = oNewName & "."
	End If
	If oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
		FromDrawing(oDoc, oNewName)
	Else
		FromPart(oDoc, oNewName)
	End If
End Sub

Sub FromDrawing(oDoc As DrawingDocument, oNewName As String)
	Dim oSheet As Sheet = oDoc.Sheets.Item(1)
	Dim pNewName As String = vbNullString
	If oSheet.DrawingViews.Count > 0 Then
		Dim oView As DrawingView = oSheet.DrawingViews.Item(1)
		Dim pDoc As Document = oView.ReferencedDocumentDescriptor.ReferencedDocument
		pNewName = SavePart(pDoc, oNewName)
		pDoc.Close(True)
	End If
	SaveDrawing(oDoc, oNewName, pNewName)
End Sub

Sub FromPart(oDoc As Document, oNewName As String)
	Dim OldName As String = oDoc.FullFileName
	Dim pNewName As String = SavePart(oDoc, oNewName)
	Dim FNP As Integer = InStrRev(OldName, ".", -1)
	OldName = Left(OldName, FNP) & "idw"
	Dim pDoc As DrawingDocument = ThisApplication.Documents.Open(OldName, True)
	SaveDrawing(pDoc, oNewName, pNewName)
End Sub

Function SavePart(oDoc As Document, oNewName As String) As String
	Dim Ext As String = GetExt(oDoc)
	If Ext = vbNullString Then Return vbNullString
	Dim NewName As String = oNewName & Ext
	oDoc.SaveAs(NewName, True)
	Return NewName
End Function

Sub SaveDrawing(oDoc As DrawingDocument, oNewName As String, pNewName As String)
	Dim NewName As String = oNewName & "idw"
	oDoc.SaveAs(NewName, False)
	oDoc.Close(True)
	If pNewName = vbNullString Then Exit Sub
	Dim nDoc As DrawingDocument = ThisApplication.Documents.Open(NewName, True)
	Dim oRefFile As Inventor.FileDescriptor = nDoc.File.ReferencedFileDescriptors(1)
	Try
		oRefFile.ReplaceReference(pNewName)
	Catch
	End Try
	nDoc.Save()
End Sub

Function GetExt(oDoc As Document) As String
	Select Case oDoc.DocumentType
	Case DocumentTypeEnum.kPartDocumentObject:	Return "ipt"
	Case DocumentTypeEnum.kAssemblyDocumentObject:	Return "iam"
	End Select
	Return vbNullString
End Function
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 11 of 13

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

Hi Martin,

 

3. rename the iam and the idw, so that the new idw is looking for the new iam

 

The above requirement can be achieved by the following sample code. It should run in Master.idw.

 

Dim oDrawingDoc as DrawingDocument
oDrawingDoc = ThisApplication.ActiveDocument

Dim NewPath As String = "C:\Temp\"

Dim NewName As String = "NewMaster"

Dim currentFile As String = ThisDoc.PathAndFileName(False)

Dim assyDoc as AssemblyDocument
assyDoc = ThisApplication.Documents.Open(currentFile & ".iam")

assyDoc.SaveAs(NewPath & NewName & ".iam",False)

assyDoc.Close()

oDrawingDoc.SaveAs(NewPath & NewName & ".idw",False)

Dim oFD As FileDescriptor
oFD = oDrawingDoc.ReferencedFileDescriptors(1).DocumentDescriptor.ReferencedFileDescriptor
oFD.ReplaceReference(NewPath & NewName & ".iam")

oDrawingDoc.Update()

 

Please feel free to contact if there is any doubt.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 12 of 13

martinhoos
Advocate
Advocate

Thank you very much, Mike and Chandra, for your help!

 

Regards Martin

0 Likes
Message 13 of 13

nivyus
Contributor
Contributor
Hello, this is awesome. It is possible to reoptimize and reassemble the IAM or idw layer by layer to facilitate modification (read the iproperty stock number in Ipt/iam as the filename) and delete the old Idw/ipt/iam. Thank you
0 Likes