Open PART in background and SAVE AS stp file

Open PART in background and SAVE AS stp file

maarten_desmet
Participant Participant
1,396 Views
4 Replies
Message 1 of 5

Open PART in background and SAVE AS stp file

maarten_desmet
Participant
Participant

Hello,

 

want it a routine that opens (in the background) an .ipt that's inside a specific .idw.. then saves  it as .stp file.

 

Little more in detail what I want:

 

I want a routine that follows these steps

1. Open .idw (manually)

2. Check the .ipt that's in that .idw

3. Open .ipt in the background

4. Save .ipt as .stp (still in background) using the .idw name.

    Location -> Same as where the .idw is saved.

5. Close .ipt in background.

 

Can someone help me with this?

 

Thanks in advance!

0 Likes
Accepted solutions (1)
1,397 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor
Accepted solution

Hi @maarten_desmet.  As with Assemblies, the documents being represented within an open drawing are also considered open (or at least initialized, which will automatically fully open when interacted with).  So, you won't need to open or close the part document.  I have an iLogic rule that I think will work for you.  You may have to read through it first and make sure all the settings are what you want first, though.  There are multiple ways to get the drawing document and its model document(s), but I'm just using the shortcut iLogic snippets here to get both.  I put the task of exporting the part into a separate Sub routine to keep the main part of the code short and clean.  This could maybe use a couple more checks here and there to make it more robust, but it should get the job done for you.

Here's the iLogic rule code:

Sub Main
	'attempt to get the first/main 'model' document of this drawing
	oModel = ThisDrawing.ModelDocument
	'if no model was found, exit the rule
	If IsNothing(oModel) Then Exit Sub
	'if it is not a Part, exit rule
	If oModel.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then Exit Sub
	'specify full file name of new file to be created by the translator
	Dim oStepFileName As String = System.IO.Path.ChangeExtension(ThisDrawing.Document.FullFileName, ".stp")
	'call our custom Sub routine below to run, supplying the needed input data
	ExportDrawingPartAsSTEP(oModel, oStepFileName)
End Sub

Sub ExportDrawingPartAsSTEP(oIPT As PartDocument, oFullFileName As String)
	'get the STEP translator add-in
	Dim oSTEP As TranslatorAddIn
	For Each oAddIn As ApplicationAddIn In ThisApplication.ApplicationAddIns
		If oAddIn.DisplayName = "Translator: STEP" Then
			oSTEP = oAddIn
		End If
	Next
	'define needed variables for translator
	Dim oTO As TransientObjects = ThisApplication.TransientObjects
	Dim oContext As TranslationContext = oTO.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	Dim oOptions As NameValueMap = oTO.CreateNameValueMap
	Dim oDataMedium As DataMedium = oTO.CreateDataMedium
	
	'set path & file name & file extension of new file to be created by translator
	oDataMedium.FileName = oFullFileName

	If oSTEP.HasSaveCopyAsOptions(oIPT, oContext, oOptions) Then
		' Set application protocol.
		 ' 2 = AP 203 - Configuration Controlled Design
		 ' 3 = AP 214 - Automotive Design
		 oOptions.Value("ApplicationProtocolType") = 3
		 oOptions.Value("IncludeSketches") = True
		 oOptions.Value("export_fit_tolerance") = .000393701
		 oOptions.Value("Author") = ThisApplication.GeneralOptions.UserName
		 oOptions.Value("Authorization") = ""
		 oOptions.Value("Description") = iProperties.Value("Project", "Description")
		 oOptions.Value("Organization") = iProperties.Value("Summary", "Company")
		Try
			 oSTEP.SaveCopyAs(oIPT, oContext, oOptions, oDataMedium)
		Catch
			MsgBox("Your attempt to export this document as a STEP file FAILED!", vbExclamation, "Export to STEP Error")
		End Try
	End If
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡 or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 5

NachoShaw
Advisor
Advisor

Hey

 

do you have any code already? To go through your itemised list-

 

1. Open .idw (manually)

This is not part of the answer needed

 

2. Check the .ipt that's in that .idw

What are you checking against? How do you intend to check that the ipt is in the idw? Im assuming the ipt is closed because you want to open it in the background in item 3. This will require the FullFileName of the ipt and the DrawingDoc.allReferencedDocuments loop to compare the path to see if it exists in the idw so, the ipt needs to be available

 

3. Open .ipt in the background

Once item 2 is qualified (you have found the ipt in the idw) you can open it in the background by using the

Document.Open(FullFileName, False) where False is the visibility of the document.

NachitoMax_1-1632841987139.png

 

4. Save .ipt as .stp (still in background) using the .idw name.

    Location -> Same as where the .idw is saved.

Then you can get the the same location as the idw from the idw path and use the translator to save as stp

NachitoMax_0-1632841931960.png

 

here is a function that will return the folder path of a file name. In this case, you would pass the FillFileName of the IDW to the function to get its folder path. Then you would need to add the name of the step file and its extension in order for the step to be saved

 

Function GetDirectory(path)
   GetDirectory = (Left(path, InStrRev(path, "\")))
End Function

and to use it

Dim FolderPath As String: FolderPath = GetDirectory(IDWFullFileName)

5. Close .ipt in background.

close the document by closing the open instance. For example:

Dim PathOfIPT As String: PathOfIPT = "C:/Temp/Test.ipt"
Dim oDoc As PartDocument

Set oDoc = ThisApplication.documents.Open(PathOfIPT, False)

Dim StepName As String: StepName = PathOfIPT.Replace("ipt", "stp")
ExportToSTEP(StepName)

oDoc Close(False)
oDoc = Nothing

 

note: i added a parameter to the ExportToSTEP sub so that i could pass the filepath i wanted. In the sub you would need to change 2 things-

 

The header

Public Sub ExportToSTEP(ByVal PathName As String)

 

the filename at the bottom of the code

oData.FileName = PathName

 

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


0 Likes
Message 4 of 5

maarten_desmet
Participant
Participant

This is (almost) exatly what I need!

 

The only thing I would like to have now, is that the .stp file is not saved in the same folder as the .idw.

It should be in for example in this structure:

 

  • LOCATION FOLDER: .idw = C:\DRAWING
  • LOCATION FOLDER: .stp = C:\DRAWING\STEPFILE

 

Can you help me get it like this?

 

Thank you very much!

0 Likes
Message 5 of 5

WCrihfield
Mentor
Mentor

Sure thing.  Again, there are several ways to do this file name manipulation task, but lately I've been favoring the 'System.IO.Path' tools, because they aren't reliant on the ThisDoc object.  But since we are using the ThisDrawing object, you could also use the ThisDoc tools too, and that should still be pointing to the same document.  It's up to your style.

Here is the updated code:

 

Sub Main
	'attempt to get the first/main 'model' document of this drawing
	oModel = ThisDrawing.ModelDocument
	'if no model was found, exit the rule
	If IsNothing(oModel) Then Exit Sub
	'if it is not a Part, exit rule
	If oModel.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then Exit Sub
	oFFN = ThisDrawing.Document.FullFileName
	oDS = System.IO.Path.DirectorySeparatorChar
	'GetDirectoryName returns full path without file name (no '\' at the end)
	oPath = System.IO.Path.GetDirectoryName(oFFN) & oDS & "STEPFILE" & oDS
	'gets file name only (no path, no file extension)
	oName = System.IO.Path.GetFileNameWithoutExtension(oFFN)
	oStepFileName = oPath & oName & ".stp"
	'call our custom Sub routine below to run, supplying the needed input data
	ExportDrawingPartAsSTEP(oModel, oStepFileName)
End Sub

Sub ExportDrawingPartAsSTEP(oIPT As PartDocument, oFullFileName As String)
	'get the STEP translator add-in
	Dim oSTEP As TranslatorAddIn
	For Each oAddIn As ApplicationAddIn In ThisApplication.ApplicationAddIns
		If oAddIn.DisplayName = "Translator: STEP" Then
			oSTEP = oAddIn
		End If
	Next
	'define needed variables for translator
	oTO = ThisApplication.TransientObjects
	oContext = oTO.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = oTO.CreateNameValueMap
	oDataMedium = oTO.CreateDataMedium
	
	'set path & file name & file extension of new file to be created by translator
	oDataMedium.FileName = oFullFileName

	If oSTEP.HasSaveCopyAsOptions(oIPT, oContext, oOptions) Then
		' Set application protocol.
		 ' 2 = AP 203 - Configuration Controlled Design
		 ' 3 = AP 214 - Automotive Design
		 oOptions.Value("ApplicationProtocolType") = 3
		 oOptions.Value("IncludeSketches") = True
		 oOptions.Value("export_fit_tolerance") = .000393701
		 oOptions.Value("Author") = ThisApplication.GeneralOptions.UserName
		 oOptions.Value("Authorization") = ""
		 oOptions.Value("Description") = iProperties.Value("Project", "Description")
		 oOptions.Value("Organization") = iProperties.Value("Summary", "Company")
		Try
			 oSTEP.SaveCopyAs(oIPT, oContext, oOptions, oDataMedium)
		Catch
			MsgBox("Your attempt to export this document as a STEP file FAILED!", vbExclamation, "Export to STEP Error")
		End Try
	End If
End Sub

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes