saving ipt and idw to a new name and location with reference replaced problem

saving ipt and idw to a new name and location with reference replaced problem

e2000773
Enthusiast Enthusiast
500 Views
5 Replies
Message 1 of 6

saving ipt and idw to a new name and location with reference replaced problem

e2000773
Enthusiast
Enthusiast

Hello i'am having a minor problem. This code fails at the last line where it's supposed to save the drawing. What could be the issue?

Dim DWGType As String = ".idw"
Dim oDoc As PartDocument = Nothing
oDoc = ThisApplication.ActiveEditDocument

Dim CurrentFilename As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullDocumentName)
Dim Path As String = System.IO.Path.GetDirectoryName(oDoc.FullDocumentName)

If Not System.IO.File.Exists(System.IO.Path.ChangeExtension(oDoc.FullDocumentName, DWGType)) Then
	Return
End If

oDoc = ThisDoc.Document

Dim oFileDlg As Inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)

oFileDlg.Filter = "Autodesk Inventor Part Files (*.*)|*.*"

oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()

oFileDlg.FileName = CurrentFilename
Try
	oFileDlg.CancelError = True
	oFileDlg.ShowSave()
Catch
	Return
End Try

If Err.Number <> 0 Then

ElseIf oFileDlg.FileName <> "" Then
	NFile = oFileDlg.FileName

	oDoc.SaveAs(NFile & ".ipt", False)

End If

Dim PNew As String = ThisDoc.WorkspacePath()

ThisApplication.Documents.Open(NFile & ".ipt")

Dim NFilename As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullDocumentName)

Dim DrawingDoc As DrawingDocument = ThisApplication.Documents.Open(Path & "\" & CurrentFilename & ".idw")

DrawingDoc = ThisApplication.ActiveEditDocument

Dim oFD As FileDescriptor = Nothing
oFD = DrawingDoc.ReferencedFileDescriptors(1).DocumentDescriptor.ReferencedFileDescriptor
oFD.ReplaceReference(NFile & ".ipt")
DrawingDoc.Update()

DrawingDoc.SaveAs(PNew & "\" & NFilename & ".idw", False)

oDoc.Activate()
0 Likes
Accepted solutions (1)
501 Views
5 Replies
Replies (5)
Message 2 of 6

A.Acheson
Mentor
Mentor

Hi @e2000773 

 

Have you checked the filepath is correct? 

 

Switch of this line also. 

 

oDoc.Activate()

Can you share the error message the more info tab and what document your in when it occurs.  

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 3 of 6

WCrihfield
Mentor
Mentor

Also, just a couple more related details that may need to be looked into.  The Intellisense hint for the 'ThisDoc.WorkspacePath' says:  "Gets the path of the workspace of the active project.  This will be empty if no workspace is defined."  If it returned nothing, then that would mess up your file path, where you are using the 'PNew' variable.  Also, at the line setting the value of the 'NFilename' variable, you are using oDoc.FullDocumentName there.  That really should be oDoc.FullFileName instead.  Because the FullDocumentName can have extra text after the FullFileName portion for various situations, such as specifying which ModelState, or which iPartMember it is referring to within "<" & ">" brackets.  That may be causing a problem in the SaveAs file path also.  Not sure though.  Also, why are you setting the value of the oDoc variable twice, using two different terms?  That is often not a good idea.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 6

e2000773
Enthusiast
Enthusiast

Yeah I forgot it was there twice when I was trying a lot of things. I fixed most of the things you mentioned and I probably made it worse now.  What am I missing here?

The error message I now get is:
System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.Documents.Open(String FullDocumentName, Boolean OpenVisible)
at ThisRule.Main() in C:\Users\m\AppData\Local\Temp\iLogic Rules\Part1.ipt.Rule1.vb:line 49
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

Before I got this which is weird because I don't have a line 72:
System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG)) at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData) at Inventor.FileDescriptor.ReplaceReference(String FullFileName) at ThisRule.Main() in C:\Users\m\AppData\Local\Temp\iLogic Rules\Part1.ipt.Rule1.vb:line 72 at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem) at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

This is the current code I have now and it now doesn't even open the drawing.

Dim DWGType As String = ".idw"
Dim oDoc As PartDocument = Nothing
oDoc = ThisApplication.ActiveEditDocument

Dim CurrentFilename As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
Dim Path As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName)

If Not System.IO.File.Exists(System.IO.Path.ChangeExtension(oDoc.FullFileName, DWGType)) Then
	Return
End If

Dim oFileDlg As Inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)

oFileDlg.Filter = "Autodesk Inventor Part Files (*.*)|*.*"

oFileDlg.InitialDirectory = "C:\"

oFileDlg.FileName = ""
Try
	oFileDlg.CancelError = True
	oFileDlg.ShowSave()
Catch
	Return
End Try

If Err.Number <> 0 Then

ElseIf oFileDlg.FileName <> "" Then
	NFile = oFileDlg.FileName

	oDoc.SaveAs(NFile & ".ipt", False)

End If

Dim PathNew As String = ThisDoc.Path()

ThisApplication.Documents.Open(NFile & ".ipt")

Dim MyNewFilename As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)

Dim DrawingDoc As DrawingDocument = ThisApplication.Documents.Open(Path & "\" & oFileDlg.FileName & ".idw")

DrawingDoc = ThisApplication.ActiveEditDocument

Dim oFD As FileDescriptor = Nothing
oFD = DrawingDoc.ReferencedFileDescriptors(1).DocumentDescriptor.ReferencedFileDescriptor
oFD.ReplaceReference(NFile & ".ipt")
DrawingDoc.Update()

DrawingDoc.SaveAs(PathNew & "\" & MyNewFilename & ".idw", False)

 

0 Likes
Message 5 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @e2000773.  I am having a hard time following exactly what you are trying to do in your code, so I am not 100% sure how to edit it to work the way you want.  I see that you are trying to find an existing drawing type file with the same path and file name as the active part document, and if it is not found, it will exit the rule.  Then it looks like you are using a SaveAs dialog to get a new path and file name for the part.  But you are only supplying the basic 'all files' filter, instead of the ".ipt" filter.  When you do that, it will return a path, file name, and a dot (.) at the end, with nothing after the dot.  Then in later lines, you are also adding a dot within the ".ipt" file extension, which will cause a problem.  Plus, there is no need to check for an error specification after that point.  If an error happened the 'Return' code on the Catch side of the Try...Catch statement would have exited the rule.  After checking that the dialog returned something, I would save that new full file name (NFile & "ipt") to a variable, because you use it again later in multiple locations.  Then you are using 'ThisDoc.Path' to set the value of the 'PathNew' variable, but that specification is very vague, and I am not sure which document you intend for that to bet the path from.  You already have the path of the 'active edit document' to the 'Path' variable above that.  So I assume you want the path of the file you specified in the dialog.  If so, use the System.IO.Path.GetDirectoryName method with the (NFile & "ipt") full file name within it, instead of 'ThisDoc.Path'.  ThisDoc.Path will likely return the path of the part that was active when you started the rule.  Then, when using the Documents.Open method, set that as the value of a Document or PartDocument type variable directly (at the beginning of the line), so you have that for later, if needed (and correct the name specified, as mentioned above).  Then in the next line you are getting the file name of the original part again, when you already have that in the 'CurrentFilename' variable.  I suspect you want the name of the new part, not the old one, so use the new variable I mentioned from the last sentence here, instead of 'oDoc'.  Next, when opening the drawing, remember about the extra dot in the file name issue.  Next, do not re-set the DrawingDoc variable's value in the next line, right after you just set its value in the 'Open' line.  You already have the correct reference.  Then in the replace reference line, remember the extra dot in the file name again.

Whew. 😅

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 6

e2000773
Enthusiast
Enthusiast

Thank you! Got it to work. Basically i just wanted a code that saves old .ipt and .idw at the same time to a new location with a new name and replaces references to the old ones.

0 Likes