Skip to main content
4-Participant
October 26, 2021
Solved

VBA ExportDXF

  • October 26, 2021
  • 1 reply
  • 2787 views

Hello everyone,

Does anyone know how to export a prt or drawing to dxf using VBA Excel? I got other activities such as changing dimensions and creating a drawing, but this one is having difficulty.

Best answer by VladimirN

1. "Creo VB API Example code to export a part model to 3D DXF Format in Creo Parametric ": https://www.ptc.com/en/support/article/CS247215

Quote: "Creo Parametric VB API Users may refer to below provided example C# function":

 

// Input arguments
// strFile = Full absolute path to the Creo Parametric part file (example: C:\Temp\dxf_export.prt)
// strDXF = DXF output file name

// Note: Passing strFile length beyond 66 chars would lead to pfcExceptions::XStringTooLongthrow.
// To use longer file paths beyond 66 char length, please see article CS95056
// conn   = Creo Asynchronous Connection object.

static internal bool ExportToDXF3D(string strFile, string strDXF, IpfcAsyncConnection conn)
{
 if (string.IsNullOrEmpty (CreoPath)) return false; 
 IpfcModel model = null;
 try { 
 var session = conn.Session as IpfcBaseSession;
 var dxfexp = (new CCpfcDXF3DExportInstructions ().Create ()) as IpfcDXF3DExportInstructions; 
 int type = (int)EpfcModelType.EpfcMDL_PART;
 var modelDesc = new CCpfcModelDescriptor ().Create (type, strFile, null);
// modelDesc.Path = "full directory path where the part file exists"	 
 model = session.RetrieveModelWithOpts (modelDesc, new CCpfcRetrieveModelOptions().Create ());
 //model.Display();
 try {
 var exi = dxfexp as IpfcExport3DInstructions;
 var triangOptions = (new CCpfcTriangulationInstructions().Create(0.10000, 0.500000)) as IpfcTriangulationInstructions;
 triangOptions.StepSize = 0.5; 
 exi.set_TriangulationOptions(triangOptions);
 var new_exi = exi as IpfcExportInstructions;
 model.Export (strDXF, new_exi);
 } catch (COMException ex) {
 var ex2 = Marshal.GetExceptionForHR (ex.ErrorCode);
 Console.WriteLine (ex2.Message);
 return false;
 }
 return true;
 } finally {
 try { if (model != null) model.Erase (); } catch { }
 conn.Close ();
 }
 return false;
}

 

2. "Example code to export (Save As) a drawing file to PDF format through Creo Parametric VB API": https://www.ptc.com/en/support/article/CS141963

Quote: "Example code to export Active drawing model to PDF format with Stroke All Fonts, Monochrome and do not check Open file in Acrobat Reader options defined": 

 

Sub Main2()
 Dim asynconn As New pfcls.CCpfcAsyncConnection
 Dim conn As pfcls.IpfcAsyncConnection
 Dim CreoSession As pfcls.IpfcBaseSession
 Dim CreoCurrentModel As pfcls.IpfcModel
 'Make an asynchronous connection with Pro/ENGINEER
 Set conn = asynconn.Connect("", "", ".", 5)
 'Get the current session
 Set CreoSession = conn.session
 'Show the current Working Directory
 MsgBox "This is the current Working Directory: " & vbCrLf & CreoSession.GetCurrentDirectory
 Set CreoCurrentModel = CreoSession.CurrentModel
 'Show the name of the Pro/E Model in a messagebox
 MsgBox "Model name = " & CreoCurrentModel.FullName

 Dim oWindow As IpfcWindow
 Set oWindow = CreoSession.GetModelWindow(CreoCurrentModel)
 'Activate the new window before printing (Good practice)
 oWindow.Activate

 Dim PDFExportInstrCreate As New CCpfcPDFExportInstructions
 Dim PDFExportInstr As IpfcPDFExportInstructions
 Set PDFExportInstr = PDFExportInstrCreate.Create
 Dim PDF_Options As New pfcls.CpfcPDFOptions

 ' Set Stroke All Fonts PDF Option
 Dim PDFOptionCreate_SAF As New CCpfcPDFOption
 Dim PDFOption_SAF As IpfcPDFOption
 Set PDFOption_SAF = PDFOptionCreate_SAF.Create
 PDFOption_SAF.OptionType = EpfcPDFOptionType.EpfcPDFOPT_FONT_STROKE
 Dim newArg_SAF As New CMpfcArgument
 PDFOption_SAF.OptionValue = newArg_SAF.CreateIntArgValue(EpfcPDFFontStrokeMode.EpfcPDF_STROKE_ALL_FONTS)
 Call PDF_Options.Append(PDFOption_SAF)

 ' Set COLOR_DEPTH value (Set EpfcPDF_CD_MONO to have Black & White output)
 Dim PDFOptionCreate_CD As New CCpfcPDFOption
 Dim PDFOption_CD As IpfcPDFOption
 Set PDFOption_CD = PDFOptionCreate_CD.Create
 PDFOption_CD.OptionType = EpfcPDFOptionType.EpfcPDFOPT_COLOR_DEPTH
 Dim newArg_CD As New CMpfcArgument
 PDFOption_CD.OptionValue = newArg_CD.CreateIntArgValue(EpfcPDFColorDepth.EpfcPDF_CD_MONO)
 Call PDF_Options.Append(PDFOption_CD)

 ' Set PDF EpfcPDFOPT_LAUNCH_VIEWER(Set FALSE Not to Launch Adobe reader)
 Dim PDFOptionCreate_LV As New CCpfcPDFOption
 Dim PDFOption_LV As IpfcPDFOption
 Set PDFOption_LV = PDFOptionCreate_LV.Create
 PDFOption_LV.OptionType = EpfcPDFOptionType.EpfcPDFOPT_LAUNCH_VIEWER
 Dim newArg_LV As New CMpfcArgument
 PDFOption_LV.OptionValue = newArg_LV.CreateBoolArgValue(False)
 Call PDF_Options.Append(PDFOption_LV)

 'Set Output PDF File Name
 PDFExportInstr.FilePath = "D:\" & CreoCurrentModel.FullName & ".pdf"
 PDFExportInstr.Options = PDF_Options
 CreoCurrentModel.Export PDFExportInstr.FilePath, PDFExportInstr
 'Disconnect with Pro/ENGINEER
 conn.Disconnect (2)
 'Cleanup
 Set asynconn = Nothing
 Set conn = Nothing
 Set session = Nothing
 Set oModel = Nothing

End Sub

 

1 reply

VladimirN24-Ruby IIIAnswer
24-Ruby III
October 26, 2021

1. "Creo VB API Example code to export a part model to 3D DXF Format in Creo Parametric ": https://www.ptc.com/en/support/article/CS247215

Quote: "Creo Parametric VB API Users may refer to below provided example C# function":

 

// Input arguments
// strFile = Full absolute path to the Creo Parametric part file (example: C:\Temp\dxf_export.prt)
// strDXF = DXF output file name

// Note: Passing strFile length beyond 66 chars would lead to pfcExceptions::XStringTooLongthrow.
// To use longer file paths beyond 66 char length, please see article CS95056
// conn   = Creo Asynchronous Connection object.

static internal bool ExportToDXF3D(string strFile, string strDXF, IpfcAsyncConnection conn)
{
 if (string.IsNullOrEmpty (CreoPath)) return false; 
 IpfcModel model = null;
 try { 
 var session = conn.Session as IpfcBaseSession;
 var dxfexp = (new CCpfcDXF3DExportInstructions ().Create ()) as IpfcDXF3DExportInstructions; 
 int type = (int)EpfcModelType.EpfcMDL_PART;
 var modelDesc = new CCpfcModelDescriptor ().Create (type, strFile, null);
// modelDesc.Path = "full directory path where the part file exists"	 
 model = session.RetrieveModelWithOpts (modelDesc, new CCpfcRetrieveModelOptions().Create ());
 //model.Display();
 try {
 var exi = dxfexp as IpfcExport3DInstructions;
 var triangOptions = (new CCpfcTriangulationInstructions().Create(0.10000, 0.500000)) as IpfcTriangulationInstructions;
 triangOptions.StepSize = 0.5; 
 exi.set_TriangulationOptions(triangOptions);
 var new_exi = exi as IpfcExportInstructions;
 model.Export (strDXF, new_exi);
 } catch (COMException ex) {
 var ex2 = Marshal.GetExceptionForHR (ex.ErrorCode);
 Console.WriteLine (ex2.Message);
 return false;
 }
 return true;
 } finally {
 try { if (model != null) model.Erase (); } catch { }
 conn.Close ();
 }
 return false;
}

 

2. "Example code to export (Save As) a drawing file to PDF format through Creo Parametric VB API": https://www.ptc.com/en/support/article/CS141963

Quote: "Example code to export Active drawing model to PDF format with Stroke All Fonts, Monochrome and do not check Open file in Acrobat Reader options defined": 

 

Sub Main2()
 Dim asynconn As New pfcls.CCpfcAsyncConnection
 Dim conn As pfcls.IpfcAsyncConnection
 Dim CreoSession As pfcls.IpfcBaseSession
 Dim CreoCurrentModel As pfcls.IpfcModel
 'Make an asynchronous connection with Pro/ENGINEER
 Set conn = asynconn.Connect("", "", ".", 5)
 'Get the current session
 Set CreoSession = conn.session
 'Show the current Working Directory
 MsgBox "This is the current Working Directory: " & vbCrLf & CreoSession.GetCurrentDirectory
 Set CreoCurrentModel = CreoSession.CurrentModel
 'Show the name of the Pro/E Model in a messagebox
 MsgBox "Model name = " & CreoCurrentModel.FullName

 Dim oWindow As IpfcWindow
 Set oWindow = CreoSession.GetModelWindow(CreoCurrentModel)
 'Activate the new window before printing (Good practice)
 oWindow.Activate

 Dim PDFExportInstrCreate As New CCpfcPDFExportInstructions
 Dim PDFExportInstr As IpfcPDFExportInstructions
 Set PDFExportInstr = PDFExportInstrCreate.Create
 Dim PDF_Options As New pfcls.CpfcPDFOptions

 ' Set Stroke All Fonts PDF Option
 Dim PDFOptionCreate_SAF As New CCpfcPDFOption
 Dim PDFOption_SAF As IpfcPDFOption
 Set PDFOption_SAF = PDFOptionCreate_SAF.Create
 PDFOption_SAF.OptionType = EpfcPDFOptionType.EpfcPDFOPT_FONT_STROKE
 Dim newArg_SAF As New CMpfcArgument
 PDFOption_SAF.OptionValue = newArg_SAF.CreateIntArgValue(EpfcPDFFontStrokeMode.EpfcPDF_STROKE_ALL_FONTS)
 Call PDF_Options.Append(PDFOption_SAF)

 ' Set COLOR_DEPTH value (Set EpfcPDF_CD_MONO to have Black & White output)
 Dim PDFOptionCreate_CD As New CCpfcPDFOption
 Dim PDFOption_CD As IpfcPDFOption
 Set PDFOption_CD = PDFOptionCreate_CD.Create
 PDFOption_CD.OptionType = EpfcPDFOptionType.EpfcPDFOPT_COLOR_DEPTH
 Dim newArg_CD As New CMpfcArgument
 PDFOption_CD.OptionValue = newArg_CD.CreateIntArgValue(EpfcPDFColorDepth.EpfcPDF_CD_MONO)
 Call PDF_Options.Append(PDFOption_CD)

 ' Set PDF EpfcPDFOPT_LAUNCH_VIEWER(Set FALSE Not to Launch Adobe reader)
 Dim PDFOptionCreate_LV As New CCpfcPDFOption
 Dim PDFOption_LV As IpfcPDFOption
 Set PDFOption_LV = PDFOptionCreate_LV.Create
 PDFOption_LV.OptionType = EpfcPDFOptionType.EpfcPDFOPT_LAUNCH_VIEWER
 Dim newArg_LV As New CMpfcArgument
 PDFOption_LV.OptionValue = newArg_LV.CreateBoolArgValue(False)
 Call PDF_Options.Append(PDFOption_LV)

 'Set Output PDF File Name
 PDFExportInstr.FilePath = "D:\" & CreoCurrentModel.FullName & ".pdf"
 PDFExportInstr.Options = PDF_Options
 CreoCurrentModel.Export PDFExportInstr.FilePath, PDFExportInstr
 'Disconnect with Pro/ENGINEER
 conn.Disconnect (2)
 'Cleanup
 Set asynconn = Nothing
 Set conn = Nothing
 Set session = Nothing
 Set oModel = Nothing

End Sub

 

4-Participant
October 27, 2021
 

Thanks, I will try to adapt the C# code to VBA.