1 Reply Latest reply on Apr 5, 2016 1:47 PM by Patrick.Cashman

    How to iterate through component via .NET(such as C#, in DxD or XDx)

    alcohol

      Hi Guys,

       

           I'm trying to iterate through component via .NET, but it does not work.

           By using "COM Interop" , I got file named "Interop.ViewDraw.dll" .

           But the code segment below could not work at all:

       

       

           ViewDraw.Application DxAppHandle = (ViewDraw.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("ViewDraw.Application");

       

           ViewDraw.IVdSchematicSheetDocuments tempSSDDxAppHandle.SchematicSheetDocuments();   // tempSSD has counter, but CANNOT iterate through

           ViewDraw.IVdObjs DesignObjects = DxAppHandle.Query(ViewDraw.VdObjectTypeMask.VDM_COMP, ViewDraw.VdAllOrSelected.VD_ALL);   // CANNOT again

       

           This problem has troubled me a few weeks, how can I fix it?

       

            Any hints are welcome!

        • 1. Re: How to iterate through component via .NET(such as C#, in DxD or XDx)
          Patrick.Cashman

          The following is for XDx:

           

          1. Add a COM reference to viewdraw in your project.

          2. Add a module to your project.  Declare the following global variables in it:

               Public app as ViewDraw.Application

               Public doc As Object

               Public dxd_running As Boolean

               Public prog_id As Integer

               Public proceed As Boolean

               Public scomps As Object

               Public scomp As ViewDraw.Component

               Public design_name As String

           

          2.1. At the top of the module, add the following import statement:

               Imports Microsoft.Win32

           

           

          3. Also in that module, add the following:

           

          Public Sub DX_Connect()

                   dxd_running = false

                   app = Nothing

                   on error goto dxd_error

                   app = GetObject(,"ViewDraw.Application" & "." & prog_id.ToString)

                   doc = app.ActiveDocument

                   dxd_running = True

           

                  

                   Exit Sub

                   dxd_error:

                   msgbox("Error: No active DxDesigner Application found.")

                  

               End Sub

              

               Public Sub get_ver()

                   Dim regkey As RegistryKey

                   Dim tmp As String

                  

                   regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Classes\MGCPCB.ExpeditionPCBApplication\CurVer", False)

                  

                   If regKey Is Nothing Then

                       tmp = "Critical registry keys are missing.  This most likely means that the Mentor Graphics "

                       tmp = tmp & "tools have not been installed properly." & vbCrLf & vbCrLf

                       tmp = tmp & "Try to correct that then start over. "

                       MsgBox(tmp)

                       proceed = false

                       exit sub

                   End If

                  

                   tmp = regkey.GetValue("")

                   tmp = tmp.Substring(InStrRev(tmp, "."))

                   prog_id = CInt(tmp)

                  

                   regKey.Close()

               End Sub

           

          4. In whatever subroutine runs when you load the main form (usually something like Main_FormLoad), add the following code:

           

                  proceed = True

                   get_ver()

                  

                   if proceed = false then

                       me.close

                   end if

                   DX_Connect()

           

          5. In whatever subroutine you have the action taking place, add the following to get all design components and iterate through them:

           

                    'load all components into scomps.  scomps will get a reference to every component in the design

                   design_name = app.GetProjectData.GetiCDBDesignRootBlock(app.GetActiveDesign())

                   scomps = nothing

                   scomps = app.DesignComponents("", design_name, "-1", , False)    'gets all the components in the design

                  

                   'cycle through all the components and get the info needed for the bom

                   For Each scomp In scomps

                              'do something on each component

                   Next

          --------------------------------------------------------------------

          There is another way to do it, which is to iterate through all the sheets, and then use the query function to select components on each sheet, then iterate through them.  I have some examples of how to do that also, if you want to do it that way.  It involves a lot more overhead because of opening all the sheets and displaying them on the screen, but in some cases that's a preferred method to doing it all behind the scenes like the method I've shown here.

           

          hope this helps.  It should get you on your way.  If you have more questions, please do not hesitate to ask.