DesignCAD 3D Max 2027 box
Now available

DesignCAD 2027

Hatches that follow their boundaries, an on-screen dial to turn your model, and a way to find any command by name, in a clean Windows 11 look.

Start DC from Visual Basic Express 2005

Started by js, September 09, 2007, 05:15:23 PM

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

js

I would like to open DesignCad 18 from a visual basic program.
Where would I find new code similar to the old code shown in devguide13.pdf?
I copied the old code into visual basic express and... I get errors about casting and whatnot.

I really would appreciate a simple snippet that just opens DesignCad in Visual Basic.

Thanks to all for their posts and responses.
js

DrollTroll

#1
Here's a sample code snippet (assumes you've added a reference to the DesignCAD type library from the COM tab of the Add Reference dialog):



' add these imports for Registry and Process bits
Imports Microsoft.Win32
Imports System.Threading

        Dim DcApp As DesignCAD.Application
        Dim DcDocs As DesignCAD.Documents
        Dim DcDoc As DesignCAD.Document
        Dim DcEntities As DesignCAD.Entities

        'Try to get the active instance, or start DesignCAD if not already running
        Try
            ' This works fine if DesignCAD already started.
            DcApp = GetObject(, "DesignCAD.Application")
        Catch ex As Exception
            Try
                ' DesignCAD doesn't seem to be running,
                ' so try starting it manually.
                ' Unfortunately New and CreateObject don't work
                ' as expected under .NET so we have to start DesignCAD
                ' another way, then wait a bit and try GetObject again.
                Dim SubKey As String = "DesignCAD.DCD-Document\shell\open\command"
                Dim rk As RegistryKey = Registry.ClassesRoot.OpenSubKey(SubKey)
                Dim DcPath As String = CType(rk.GetValue("", ""), String)
                Dim DcPath2 = DcPath.Remove(DcPath.Length - 5, 5)
                Process.Start(DcPath2)
                Thread.Sleep(2500)
                DcApp = GetObject(, "DesignCAD.Application")
                DcApp.Visible = True
                DcApp.BringToTop()
            Catch ex2 As Exception
                MsgBox(ex2.Message & Chr(13) & "Unable to connect to DesignCAD. Please start DesignCAD manually and try again.")
                DcApp = Nothing
                Me.Close()
                Exit Sub
            End Try
        End Try
        Try
            DcApp.Visible = True
        Catch
            Me.BringToFront()
            MsgBox("Error: " & Err.Description & Chr(13) & " Problem connecting to DesignCAD. Exiting...")
            DcApp = Nothing
            Exit Sub
        End Try

        DcApp.BringToTop()
        DcDocs = DcApp.Documents()
        ' I thought Open wasn't working at first, but then I realized
        ' I had forgotten to add the "*\" in front of the filename
        ' Feel free to change the document filename for something on your system...
        DcDoc = DcDocs.Open("*\JustADrawing.dcd")
        If Not (DcDoc Is Nothing) Then
            DcEntities = DcDoc.Entities()
            Dim count As Integer
            count = DcEntities.Count()
            Me.BringToFront()
            MsgBox("Entity count: " & count)
        End If
        DcEntities = Nothing
        DcDoc = Nothing
        DcDocs = Nothing
        DcApp = Nothing


Caveat: I am assuming this will work from Visual Basic Express 2005 -- it was originally written using the Visual Studio 2005 Professional.
25 years with DesignCAD

js

Thanks DT,
The code is more complicated than i expected.
I will try it soon.
Thanks for the quick reply
js

DrollTroll

Yes, unfortunately, for some reason .NET doesn't recognize the DesignCAD.Application object as something it can readily create. We're still trying to figure that out as time allows.
25 years with DesignCAD

js

DT,
Your code does work with Visual Basic Express.
After reading the procedure, I think I had better stick with reading and writing data files between programs.
Thanks again,
js

dcadRob

Take these line out of DT's code :

Imports Microsoft.Win32
Imports System.Threading

Instead go into Solution Explorer->My project-> Project Designer->References->Imported Namespace
and make sure Microsoft.Win32 and System.Threading have checkmarks next to them.