STP export

STP export

Anonymous
Not applicable
813 Views
6 Replies
Message 1 of 7

STP export

Anonymous
Not applicable

Hi all!

 

I'm having trouble writing an ilogic code that exports a part or sheet metal part to a given location.  The ideal code should prompt the user if the name is correct with a message box 'Yes or No'.  It should also prompt user if there is already a file with the given name, and if so, does the user choose to override it.  The given name comes from a custom iproperty called 'Item Code'.  Lastly, if the iproperty has the character ` in the Revision Number field, then it just names it 'Item Code value', if it has anything else written in 'Revision Number' field, then it prints 'Item code + Revision Number' as the name.

 

I tried writing it using code as a base, and had to many errors, couldn't figure it out:

 

'Dim title As String = "converted to title case"
'    Console.WriteLine(StrConv(title, VbStrConv.ProperCase))
oModelDoc = IO.Path.GetFileName(ThisDrawing.ModelDocument.FullFileName)

DrawingName = iProperties.Value(oModelDoc, "Custom", "Item Code")
DesiredPath = "C:\JACKRABBIT\INVENTOR TEMPLATES 2022\MISC\PDF"

NewFileName = DesiredPath & "\" & DrawingName & ".pdf"


'check file name to see if it is used
	'if it is, ask the user to overwrite
	'if it's not, save the file

question = MessageBox.Show("Is this a good file name?:  " & DrawingName & ".pdf", "iLogic Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question)

If question=vbYes Then

	If System.IO.File.Exists(NewFileName) Then
	
		question = MessageBox.Show("File exists, do you want to overwrite?", "iLogic Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
	
			If question = vbYes Then 
				System.IO.File.Delete(NewFileName)
				ThisDoc.Document.SaveAs(NewFileName, True)
			Else If question =vbNo Then
				MessageBox.Show("File was not overwritten", "iLogic Message")
			End If
	
	Else 
		
		ThisDoc.Document.SaveAs(NewFileName, True)
		
	End If 

Else If question=vbNo Then
	MessageBox.Show("Please fix Part Number")
End If

 

0 Likes
Accepted solutions (1)
814 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

I think I've got something you can try.

Make sure you read through it thoroughly before running it, to make sure everything seems OK to you.

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("A Drawing Document must be active for this rule to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If
	Dim oDDoc As DrawingDocument = ThisApplication.ActiveDocument
	Dim oMDoc As Document = oDDoc.ReferencedDocuments.Item(1)
	Dim oPath As String = "C:\JACKRABBIT\INVENTOR TEMPLATES 2022\MISC\STP\"
	Dim oItemCode As String = iProperties.Value(oMDoc.DisplayName, "Custom", "Item Code")
	Dim oNewFileName As String = oPath & oItemCode & ".stp"

	'confirm file name is OK with user
	oNameCheck = MsgBox("The following file name is available:" & vbCrLf & oNewFileName & vbCrLf & "Is this OK?", vbYesNo + vbQuestion, "")
	If oNameCheck = vbNo Then
		oNewFName = InputBox("Please enter a new file name (without path or extension) for the new file.", "Specify Name")
		If oNewFName = "" Then
			Exit Sub
		Else
			oNewFileName = oPath & oNewFName & ".stp"
		End If
	End If

	'check if file already exists
	If System.IO.File.Exists(oNewFileName) Then
		oOverWrite = MsgBox("There is already a file named:" & vbCrLf & oNewFileName & vbCrLf & "Do you want to overwrite it?", vbYesNo + vbQuestion, "")
		If oOverWrite = vbNo Then
			Exit Sub
		ElseIf oOverWrite = vbYes Then
			System.IO.File.Delete(oNewFileName)
		End If
	End If
	'call the Sub to export it to STEP
	ExportSTEP(oMDoc, oNewFileName)
End Sub

Sub ExportSTEP(oDoc As Document, oFullName As String)
	Dim oSTEP As TranslatorAddIn
	For Each oAddIn As ApplicationAddIn In ThisApplication.ApplicationAddIns
		If oAddIn.DisplayName = "Translator: STEP" Then
			oSTEP = oAddIn
		End If
	Next
	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
	oDataMedium.FileName = oFullName

	If oSTEP.HasSaveCopyAsOptions(oDoc, 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
		 ' Other options...
		 'oOptions.Value("SplineFitAccuracy") = .000393701  'I don't know if this line works.
		 oOptions.Value("export_fit_tolerance") = .000393701  'This Option name is definately recognized
		 oOptions.Value("Author") = ThisApplication.GeneralOptions.UserName
		 oOptions.Value("Authorization") = ""
		 oOptions.Value("Description") = iProperties.Value(oDoc.DisplayName, "Project", "Description")
		 oOptions.Value("Organization") = iProperties.Value(oDoc.DisplayName, "Summary", "Company")
		Try
			 oSTEP.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
		Catch
			MsgBox("Your attempt to export the model document as a STEP file FAILED!", vbOKOnly+vbCritical, "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 have time, please... Vote For My IDEAS 💡or you can Explore My CONTRIBUTIONS

Inventor 2021 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 7

Anonymous
Not applicable

Can you edit it to be used from the part file rather than the drawing ?

0 Likes
Message 4 of 7

WCrihfield
Mentor
Mentor

OK. Try this.

But I just realized it doesn't check the revision property yet.

I'll try to remember to revisit this tomorrow.

Here's the Part code for now.

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kPartDocumentObject Then
		MsgBox("A Part Document must be active for this rule to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If
	Dim oPDoc As PartDocument = ThisApplication.ActiveDocument
	Dim oPath As String = "C:\JACKRABBIT\INVENTOR TEMPLATES 2022\MISC\STP\"
	Dim oItemCode As String = iProperties.Value(oPDoc.DisplayName, "Custom", "Item Code")
	Dim oNewFileName As String = oPath & oItemCode & ".stp"

	'confirm file name is OK with user
	oNameCheck = MsgBox("The following file name is available:" & vbCrLf & oNewFileName & vbCrLf & "Is this OK?", vbYesNo + vbQuestion, "")
	If oNameCheck = vbNo Then
		oNewFName = InputBox("Please enter a new file name (without path or extension) for the new file.", "Specify Name")
		If oNewFName = "" Then
			Exit Sub
		Else
			oNewFileName = oPath & oNewFName & ".stp"
		End If
	End If

	'check if file already exists
	If System.IO.File.Exists(oNewFileName) Then
		oOverWrite = MsgBox("There is already a file named:" & vbCrLf & oNewFileName & vbCrLf & "Do you want to overwrite it?", vbYesNo + vbQuestion, "")
		If oOverWrite = vbNo Then
			Exit Sub
		ElseIf oOverWrite = vbYes Then
			System.IO.File.Delete(oNewFileName)
		End If
	End If
	'call the Sub to export it to STEP
	ExportSTEP(oPDoc, oNewFileName)
End Sub

Sub ExportSTEP(oDoc As Document, oFullName As String)
	Dim oSTEP As TranslatorAddIn
	For Each oAddIn As ApplicationAddIn In ThisApplication.ApplicationAddIns
		If oAddIn.DisplayName = "Translator: STEP" Then
			oSTEP = oAddIn
		End If
	Next
	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
	oDataMedium.FileName = oFullName

	If oSTEP.HasSaveCopyAsOptions(oDoc, 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
		 ' Other options...
		 'oOptions.Value("SplineFitAccuracy") = .000393701  'I don't know if this line works.
		 oOptions.Value("export_fit_tolerance") = .000393701  'This Option name is definately recognized
		 oOptions.Value("Author") = ThisApplication.GeneralOptions.UserName
		 oOptions.Value("Authorization") = ""
		 oOptions.Value("Description") = iProperties.Value(oDoc.DisplayName, "Project", "Description")
		 oOptions.Value("Organization") = iProperties.Value(oDoc.DisplayName, "Summary", "Company")
		Try
			 oSTEP.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
		Catch
			MsgBox("Your attempt to export the model document as a STEP file FAILED!", vbOKOnly+vbCritical, "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
Message 5 of 7

Anonymous
Not applicable

For some reason it's giving me this error:

 

 

image.png

 

Sheet 3 is a saved file. It is a sheet metal part (I also tried it on a standard part template).

0 Likes
Message 6 of 7

WCrihfield
Mentor
Mentor
Accepted solution

As soon as I saw that, I instantly know where the problem was.

Now that the Part document is the 'active' document during the whole process, we no longer need to use the document name within the iProperty.Value() calls.  I forgot to change those instances within the code.  (I posted that code at the literal last minute of my shift at work, so it seems I was a bit to hasty.)

So, I just eliminated that input variable in those 3 locations.

I also included the revision number check and the file name variations accordingly.

Try it now.

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kPartDocumentObject Then
		MsgBox("A Part Document must be active for this rule to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If
	Dim oPDoc As PartDocument = ThisApplication.ActiveDocument
	Dim oPath As String = "C:\JACKRABBIT\INVENTOR TEMPLATES 2022\MISC\STP\"
	Dim oItemCode As String = iProperties.Value("Custom", "Item Code")
	Dim oRev As String = iProperties.Value("Summary", "Revision Number")
	Dim oNewFileName As String
	If oRev.Contains("`") Then
		oNewFileName = oPath & oItemCode & ".stp"
	Else
		oNewFileName = oPath & oItemCode & oRev & ".stp"
	End If

	'confirm file name is OK with user
	oNameCheck = MsgBox("The following file name is available:" & vbCrLf & oNewFileName & vbCrLf & "Is this OK?", vbYesNo + vbQuestion, "")
	If oNameCheck = vbNo Then
		oNewFName = InputBox("Please enter a new file name (without path or extension) for the new file.", "Specify Name")
		If oNewFName = "" Then
			Exit Sub
		Else
			oNewFileName = oPath & oNewFName & ".stp"
		End If
	End If

	'check if file already exists
	If System.IO.File.Exists(oNewFileName) Then
		oOverWrite = MsgBox("There is already a file named:" & vbCrLf & oNewFileName & vbCrLf & "Do you want to overwrite it?", vbYesNo + vbQuestion, "")
		If oOverWrite = vbNo Then
			Exit Sub
		ElseIf oOverWrite = vbYes Then
			System.IO.File.Delete(oNewFileName)
		End If
	End If
	'call the Sub to export it to STEP
	ExportSTEP(oPDoc, oNewFileName)
End Sub

Sub ExportSTEP(oDoc As Document, oFullName As String)
	Dim oSTEP As TranslatorAddIn
	For Each oAddIn As ApplicationAddIn In ThisApplication.ApplicationAddIns
		If oAddIn.DisplayName = "Translator: STEP" Then
			oSTEP = oAddIn
		End If
	Next
	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
	oDataMedium.FileName = oFullName

	If oSTEP.HasSaveCopyAsOptions(oDoc, 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
		 ' Other options...
		 'oOptions.Value("SplineFitAccuracy") = .000393701  'I don't know if this line works.
		 oOptions.Value("export_fit_tolerance") = .000393701  'This Option name is definately recognized
		 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(oDoc, oContext, oOptions, oDataMedium)
		Catch
			MsgBox("Your attempt to export the model document as a STEP file FAILED!", vbOKOnly+vbCritical, "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)

Message 7 of 7

Anonymous
Not applicable

Beautiful.

0 Likes