9 Replies Latest reply on May 3, 2016 7:13 AM by fuba

    Using VB script to export IDF and DXF formats from Expedition PCB

    fuba

      Hi all,

       

      I am looking for a way to export IDF and DXF via VB script automation.

      I found this thread here,

      https://communities.mentor.com/message/38391#comment-38391

      but I am not able to get how its done. Are there other ways existing since 2012?

       

      I am on Expedition - VX1.

       

      Does also someone have experience within Expedition PCB with

      "pcbDoc.CCZExportControl" or "EDXExportControl" how do I

      handle these Objects, what can I do with them???

       

      Thanks in advance

        • 1. Re: Using VB script to export IDF and DXF formats from Expedition PCB
          Patrick.Cashman

          The following subroutine will export DXF data from a userlayer (MY_USER_LAYER) to a DXF file ( you supply the 'file' path as a string from wherever this routine is called.)

           

          Public Sub create_dxf_file(file As String)

                  

                   Dim dxfeng As MGCPCBEngines.DXFExport

                   dxfeng = CreateObject("MGCPCBEngines.DXFExport")

                  

                   If dxfeng Is Nothing Then

                       MsgBox("dxfeng is nothing")

                       Exit Sub

                   End If

                  

                   dxfeng.DesignFileName = pcbdoc.FullName

                   dxfeng.FillPads = False

                   dxfeng.MirrorOutput = False

                   dxfeng.PutElementExport(150, "0", "MY_USER_LAYER", True)

                   dxfeng.OutputFileName = file

                  

                   dxfeng.Go

                  

                   dxfeng = Nothing

          End Sub

           

          Take note - this is done in vb.net, not vbscript.  You may need to make minor adjustments for that. There are examples in the help on automation engines on how to set them up.

          2 of 2 people found this helpful
          • 2. Re: Using VB script to export IDF and DXF formats from Expedition PCB
            Patrick.Cashman

            Here is a subroutine that will export the IDF data. Same kind of deal - you have to feed it the file paths for the two output files.

             

            Sub create_idf_output(emn_output_path as string, emp_output_path as string)

                    

                     Dim idf_out_engine As MGCPCBEngines.IDFExport

                            

                     'here we create the engine object

                     idf_out_engine = CreateObject("MGCPCBEngines.IDFExport")

                    

                     'now we tell the engine what pcb file to use

                     idf_out_engine.DesignFileName = pcbdoc.FullName

                    

                     'we tell the engine where to put the output file

                     idf_out_engine.BoardOutputFile = emn_output_path`

                    

                     'we tell the engine where to put the output library file

                     idf_out_engine.LibraryOutputFile = emp_output_path

                    

                    

                     idf_out_engine.UseBoardThicknessFromStackup = true

                     idf_out_engine.DefaultComponentHeight = 0

                     idf_out_engine.ExcludeCellRouteObstructs = False

                     idf_out_engine.ExportComponentPinAndViaDrilledHoles = True

                     'idf_out_engine.ExportSection(7) = true

                     idf_out_engine.ExportSection(5) = True

                     idf_out_engine.ExportSection(4) = True

                     idf_out_engine.ExportSection(6) = True

                     idf_out_engine.ExportSection(3) = True

                     'idf_out_engine.ExportSection(8) = True

                    

                     Try

                         idf_out_engine.Go

                     Catch ex As Exception

                         msgbox("An Automation Pro License was not found. Just try again.", vbOKOnly)

                         Exit Sub

                        

                     End Try

                    

                    

                     idf_out_engine = Nothing

                

            End Sub

             

            Take note - you can also use the Variant Manager to get a list of components that are not installed, if you use that.  I also have a complete script that with several subroutines that demonstrate how to get all this information from the variant manager, then create the IDF files, then modify them so that downstream tools such as CircuitWorks (part of SolidWorks) can import them and the uninstalled parts will be correctly designated as unplaced.  Posting all of that here would be a hassle, but if you send me your email through a message here I'll send it to you.

            2 of 2 people found this helpful
            • 3. Re: Using VB script to export IDF and DXF formats from Expedition PCB
              fuba

              Hello Patrick,

              thanks for the good pieces of code. I diddnt know that something like this ie existing, so I am happy.

              My focus is also on .Net (vb or c#), so I can directely start working with your examples.

               

              Then I found on the local "Info Hub" the documentation. about "PCB Export Engine."

               

              I referenced the dll or tlb (MGCPCBEngines.dll / MGCPCBEngines.tlb) from

              1. C:\ProgramData\mgcSDD\win32\TLB
                like mentioned here: EEVX.1: Need help registering 64bit MGCPCBEngines Type Library in Visual Basic .Net
              2. C:\MentorGraphics\EEVX.1\SDD_HOME\common\win64\lib

               

              But I am not able to perform any Export.

              It seems to me that I have an issue with registration of the COM DLL.

               

              I went also through that Thread, especially the attached pdf.

              ExtendedPrint not working after installing xpedition

              But still no success.

               

              I tried running one of the scripts I found here

              C:\MentorGraphics\EEVX.1\SDD_HOME\standard\examples\pcb\Automation\AutoProEngines.

              The "RunDXFExport.vbs" is running without any issues. So I think I have a "Automation Pro License"

              but it seems that I have problems with the COM (32/64 bit) interaction.

               

              codeexample-dxf.jpg

               

              I tried compiling x64, x32, anyCPU, but no success.Pls see below my Expedition Version

               

              expedInfo.png

               

              Do you (or someone else) have any experience here what may be gone wrong ???

               

              Thanks in advance.

               

              Stefan

              • 4. Re: Using VB script to export IDF and DXF formats from Expedition PCB
                Patrick.Cashman

                There are a number of ways this can be tripped up with the VX release.  Here is how I would attack it:

                 

                1. Make sure your project has references to the COM objects from the current release of Xpedition. In this case  you will need MGCPCBEngines, as well as MGCPCB.  I do this by using the Add Reference function and selecting those from the COM object list.  (BTW I use the Sharp Develop IDE most of the time.)

                 

                2. You will need to start your IDE with mglaunch.  I do this by way of a batch file with the following:

                %mglaunch% D:\my_path\my_project.sln

                and I have set an environment variable called mglaunch with the path to the current version's mglaunch.  This is found typically in the following location:

                C:\MentorGraphics\EEVX.1.2\SDD_HOME\common\win32\bin\mglaunch.exe

                or win64 if you have 64bit installed. Of course it will change with different releases.

                 

                The reason you need to do this is that the PCB Engines must start blindly from within your executable, and they do not attach to a running session of one of the Mentor tools.  Therefore your executables must be started from mglaunch, and thus your IDE must also be started from mglaunch.  The way you know if you need to launch with mglaunch or not is if you have any CreateObject statements in your code for starting applications.  If you do, you must start the IDE with mglaunch, and you must also start your executable (see below) with it as well.  The other option is the GetObject statement.  If you only have those in your code, to attach to existing sessions of the Mentor tools, mglaunch is not needed.

                 

                3.  If mglaunch is needed (which in this case, with MGCPCB Engines, it is), you must start your executable with it.  Mentor recommends using the ReleaseEnvironmentServer for this purpose.  Naturally I do it a different way. My was was suggested by someone from Mentor who wrote a document on various ways of doing this, so it's not like I just said, "screw you guys, I'm doing it another way!"  No, I followed their advice and it worked.  It's also simpler and uses less code, so I like it.

                 

                I start the automation executable with a vbs script that gets the path to mglaunch from the Windows registry. It looks like this: (filename is something like start.vbs)

                 

                Option explicit

                 

                run

                 

                sub run()

                     Dim objShell, regkey, mglaunch

                    

                     Set objShell = CreateObject("WScript.Shell")

                     regkey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\mglaunch.exe\"

                     mglaunch = objShell.RegRead(regkey)

                     mglaunch = mglaunch & " " & chr(34) & "D:\my_path\my_executable.exe" & chr(34)

                     objShell.run mglaunch

                    

                     set objShell = Nothing

                End sub

                 

                This script gets the path of the currently registered version of mglaunch from the registry, then uses that path to run mglaunch with the executable as the argument.

                 

                So, get all that in place, and see where it gets you.  The other element which may be missing, you already mentioned - Automation Pro license.  You also need that to run the MGCPCBEngines. I can tell you that with the above items in place, I am able to start and run the Engines with the scripts above.  The system does work if you have everything in place correctly.

                1 of 1 people found this helpful
                • 5. Re: Using VB script to export IDF and DXF formats from Expedition PCB
                  fuba

                  Hey Patrick,

                   

                  you made it !!! I have my Macros up and running.

                  How do you know ? What kind of Support do I need to get this information from Mentor ?

                  Is there some Documentation I can find this in ?

                   

                  Regarding your Script To Launch the ".exe", I created a Macro in my tool, which sets up this script, if it is not there.

                  Makes it easier for me when distributing the tool. I attache it below for others.

                   

                    Sub createLauncherScript()
                  
                          Dim thisAppPath As String
                          thisAppPath = Application.StartupPath
                  
                          Dim thisAppName As String
                          thisAppName = Application.ProductName
                          thisAppName = IO.Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().Location) ' the *.exe Name
                  
                          Dim ffn As String ' --> ffn = Full File Name
                          ffn = IO.Path.Combine(thisAppPath, thisAppName)
                  
                          Dim sb As StringBuilder
                          sb = New StringBuilder
                  
                          With sb
                              .AppendLine("Option Explicit")
                              .AppendLine()
                              .AppendLine("#")
                              .AppendLine("# Script Automatically Created by: " & thisAppName)
                              .AppendLine("#")
                              .AppendLine()
                              .AppendLine("Call run")
                              .AppendLine()
                              .AppendLine("Sub run()")
                              .AppendLine()
                              .AppendLine("Dim objShell")
                              .AppendLine("Set objShell = CreateObject('WScript.Shell')")
                              .AppendLine()
                              .AppendLine("Dim regKey")
                              .AppendLine("regKey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\mglaunch.exe\'")
                              .AppendLine()
                              .AppendLine("Dim mglaunch")
                              .AppendLine("mglaunch = objShell.RegRead(regkey)")
                              .AppendLine()
                              .AppendLine("mglaunch = mglaunch & ' ' & Chr(34) & '" & ffn & "' & Chr(34)")
                              .AppendLine("objShell.run(mglaunch)")
                              .AppendLine()
                              .AppendLine("set objShell = Nothing")
                              .AppendLine()
                              .AppendLine("End Sub")
                          End With
                  
                          Dim vbsCode As String
                          vbsCode = sb.ToString.Replace("'", Chr(34))
                          vbsCode = vbsCode.Replace("#", "'#")
                  
                          ffn = IO.Path.Combine(thisAppPath, IO.Path.GetFileNameWithoutExtension(thisAppName) & ".vbs")
                          My.Computer.FileSystem.WriteAllText(ffn, vbsCode, False, System.Text.Encoding.ASCII)
                  
                          MsgBox("Ready")
                  
                      End Sub
                  

                   

                  Thanks

                   

                  Stefan

                   

                  Ps.: I am using Visual Studio, as an IDE, works also very well.

                  • 6. Re: Using VB script to export IDF and DXF formats from Expedition PCB
                    Patrick.Cashman

                    How do you know ? What kind of Support do I need to get this information from Mentor ?

                    Is there some Documentation I can find this in ?

                     

                    In the help files that come with VX, there is the Common Automation Reference.  In there is a section called Scripting with Multiple Installs. Some of the information is in there.  Most of it has to do with the Release Environment Server.  I find their examples way too complicated and they have a lot more stuff in them than is needed to get the job done.

                     

                    There is also a Word document located in

                     

                    C:\MentorGraphics\EEVX.1.2\SDD_HOME\standard\examples\pcb\Automation\ReleaseEnvServerExamples

                     

                    called MGCPCBVersionedAutomation.docx.  It has some useful information in it.  It was the thing that got me started looking in the registry for keys that have to do with the Mentor installations.

                     

                    Other than that it was many many hours of trial and error, and some advice from Jeremy Parker that helped me sort it all out. 

                     

                    If you have more questions, post them up here.  I am happy to help out with whatever I know.

                    • 7. Re: Using VB script to export IDF and DXF formats from Expedition PCB
                      fuba

                      Hi, its me again.

                       

                      Thanks for all the support so far, now I have my Tool up and running, doing various exports for me.

                      My problem is now the DXF-Engine / Exporter.

                       

                      I want to export all the Traces from the PCB Layers.

                       

                      Below a cutout of my Macro, which gives me not the results I am expecting. It only exports me the last Layer.

                      I tried playing around with the layer names

                      • passing only a number
                      • passing "Traces Layer 1", "Traces Layer 2", ... (like mentioned in the DXF-Export Window)
                      • passing "SIGNAL_1", "SIGNAL_2", ...(like mentioned in the xPCB Layout GUI)

                      But this all does not work well.

                       

                      The best is, when I load the Scheme File, but this is something, I dont want for a few other reasons

                      (because it is static, does not include a solution for various PCB's with different amount of layers, ...)

                       

                      Has anyone an idea here, what I am doing wrong ?

                      Why do I have different Layer Names (see image below), between the Display and the DXF Exporter ?

                       

                      dxfExp-Layers.png

                       

                      Now the Macro I am using.

                       

                        Public Shared Sub export_dxf()
                      
                              '#
                              '# The usage of these engines through Automation 
                              '# requires an Automation Pro license with the 
                              '# exception of Drawing File Wizard. 
                      
                              '# Make a Reference to: 
                              '# C:\MentorGraphics\EEVX.1\SDD_HOME\common\win64\lib\MgcPcbEngines.tlb or .dll
                      
                              '# Connect via mgLaunch.exe
                              
                              Dim sb As StringBuilder
                              sb = New StringBuilder
                              With sb
                                  .AppendLine()
                                  .AppendLine("Start: " & Now.ToString)
                                  .AppendLine("dxf File creation")
                              End With
                              frmMain.UpdateRtb(sb.ToString, Color.BlueViolet)
                      
                              Dim dxfEngine As MGCPCBEngines.DXFExport
                              dxfEngine = New MGCPCBEngines.DXFExport
                              
                              If dxfEngine Is Nothing Then
                                  MsgBox("Cannot obtain an Automation Object")
                                  Exit Sub
                              End If
                      
                              Dim outPath As String
                              outPath = IO.Path.Combine(pcbDoc.Path, "output", "DXF")
                      
                              If not IO.Directory.Exists(outPath) Then
                                  '# Create The Folder(s)
                                  IO.Directory.CreateDirectory(outPath)
                              End If
                      
                              Dim fn As String 'FileNAme
                              fn = IO.Path.GetFileNameWithoutExtension(pcbDoc.Name) & ".dxf"
                      
                              Dim ffn As String 'FullFileName
                              ffn = IO.Path.Combine(outPath, fn)
                      
                              With dxfEngine
                                  
                                  .DesignFileName = pcbDoc.FullName
                      
                                  '# This works best, having a Scheme File, and export it
                                  '.Scheme(MGCPCBEngines.EEnginesSchemeType.eengSchemeTypeUser) = IO.Path.Combine(Application.StartupPath, "myDxf.edxf")
                      
                                  Call exportLayers(dxfEngine)
                      
                                  If 0 Then
                                      '# section switched off for test of "exportLayers" Function
                                      .FillPads = True
                                      .MirrorOutput = False
                                      .BoardContents = True
                      
                                      .DXFUnits = MGCPCBEngines.EEnginesUnit.eengUnitMM
                      
                                      Dim layerName As String
                                      layerName = "SIGNAL_1"
                      
                                      Dim dxfLayerName As String
                                      dxfLayerName = "Lay-0"
                      
                                      .PutElementExport(MGCPCBEngines.EEnginesElementType.eengElementTypeAssemblyRefDesTop, dxfLayerName, layerName, True)
                                      .PutElementExport(MGCPCBEngines.EEnginesElementType.eengElementTypeTraces, dxfLayerName, layerName, True)
                      
                                  End If
                      
                                  .OutputFileName = ffn
                              End With
                      
                              Try
                                  dxfEngine.Go()
                              Catch ex As Exception
                                  MsgBox(ex.ToString)
                              End Try
                      
                              Call clsExport_utils.ListErrors(dxfEngine.Errors)
                      
                              dxfEngine = Nothing
                      
                              sb = New StringBuilder
                              With sb
                                  .AppendLine()
                                  .AppendLine("dxf - 'Drawing Interchange File Format' File")
                                  .AppendLine("Created here:")
                                  .AppendLine(clsUtils.createLink(outPath))
                              End With
                      
                              frmMain.UpdateRtb(sb.ToString, Color.BlueViolet)
                      
                          End Sub
                          
                            Private Shared Sub exportLayers(dxfEngine As MGCPCBEngines.DXFExport)
                      
                              Dim layerName As String
                              layerName = "dummy"
                      
                              Dim dxfLayerName As String
                              dxfLayerName = "dummy"
                      
                              Dim layIdx As Integer
                              layIdx = 0
                      
                              For i = 1 To dxfEngine.LayerCount
                      
                                  layerName = "Traces Layer " & i
                      
                                  dxfLayerName = "L_" & i.ToString("00")
                      
                                  Try
                                      dxfEngine.PutElementExport(MGCPCBEngines.EEnginesElementType.eengElementTypeTraces, dxfLayerName, layerName, True)
                                  Catch ex As Exception
                                      Console.WriteLine("cannot export: " & layerName & " -- " & dxfLayerName & " ---> " & ex.Message.ToString)
                                  End Try
                      
                              Next
                      
                          End Sub
                          
                            Public Shared Sub ListErrors(Errors As MGCPCBEngines.Errors)
                      
                              Dim sbErr As StringBuilder
                              sbErr = New StringBuilder
                      
                              Dim oError As MGCPCBEngines.Error
                              For Each oError In Errors
                      
                                  Select Case oError.Severity
                                      Case MGCPCBEngines.EEnginesErrorSeverity.eengError
                      
                                      Case MGCPCBEngines.EEnginesErrorSeverity.eengFatalError
                      
                                      Case MGCPCBEngines.EEnginesErrorSeverity.eengWarning
                      
                                      Case Else
                      
                                  End Select
                      
                                  sbErr.AppendLine(oError.Severity.ToString & " - " & oError.ErrorString)
                      
                              Next
                      
                              frmMain.UpdateRtb(sbErr.ToString, Color.Red)
                      
                          End Sub
                      

                       

                       

                      Thanks Stefan

                      • 8. Re: Using VB script to export IDF and DXF formats from Expedition PCB
                        matthias.cosaert

                        Hi,

                         

                        Using the layer number should work but it has to be passed as an integer type which might have been the issue if you used layername = i.

                         

                        Then it will be sent as a string type so the export engine will think it is the layer name instead of the layer number. 

                        try using:
                        dxfEngine.PutElementExport(MGCPCBEngines.EEnginesElementType.eengElementTypeTraces, dxfLayerName, i, True) 

                        • 9. Re: Using VB script to export IDF and DXF formats from Expedition PCB
                          fuba

                          Matthias, thanks.

                           

                          that was the Trick. I coded, and coded, and ...

                          Anyway, thanks for the input, now it is working fine.