VB.NET Express2005 ACADv05

Discussion in 'AutoCAD' started by Paul Richardson, Sep 18, 2004.

  1. I wrote a test .exe which fires ADT05 then draws a line and
    a circle..zooms..ect.

    Code fires from a button and works fine; only I was having a problem with
    ADT firing multiple times(two or three) after clicking the button, but
    before my code even got to execute the buttons click event.[which is the
    only section of code which fires ACAD].

    Object were not created in those versions. Finally click event would execute
    creating another version with all objects drawn as expected.

    I found the problem to be in this line:
    acadApp = GetObject(, "AutoCAD.Application")'correct

    Which I had accidently typed as: ("" added)
    acadApp = GetObject("", "AutoCAD.Application")'wrong

    Once "" were removed problem was solved. Could someone please explain what
    was happening? Attached segment below; which works fine..

    TFYT..
    Paul
    Private Sub Command1_Click(ByVal eventSender As System.Object, _

    ByVal eventArgs As System.EventArgs) Handles Command1.Click

    On Error Resume Next

    ' Connect to the AutoCAD application

    Dim acadApp As Autodesk.AutoCAD.Interop.AcadApplication

    acadApp = GetObject(, "AutoCAD.Application")

    If Err.Number Then

    Err.Clear()

    acadApp = CreateObject("AutoCAD.Application")

    If Err.Number Then

    MsgBox(Err.Description)

    Exit Sub

    End If

    End If
     
    Paul Richardson, Sep 18, 2004
    #1
  2. Paul Richardson

    bcoward Guest

    Paul,

    When using the GetObject and CreateObject methods your telling your app that your going to use a reference to an ActiveX object. Your either going to supply the pathname or class for the object.

    "" was saying, obtain a reference to this object from this ("") pathname. Since "" wasn't something like C:\Program Files\CAD\ACAD.exe nothing happened for you. Once you added the second optional parameter (AutoCAD.Application) your app knew what to do.

    It went to \HKEY_LOCAL_MACHINE\Software\CLASSES\AutoCAD.Application hive and looked at the CLSID to see what object was going to be started. CLSID revealed that the object being referenced could be 8E75D11-3D21-11d2-85C4-080009A0C626, thus start me now.

    Hope this helps,

    Bob Coward
    CADS, Inc

    800-366-0946
     
    bcoward, Sep 20, 2004
    #2
  3. Nice explanation Bob, Thank You...
    that your going to use a reference to an ActiveX object. Your either going
    to supply the pathname or class for the object.
    Since "" wasn't something like C:\Program Files\CAD\ACAD.exe nothing
    happened for you. Once you added the second optional parameter
    (AutoCAD.Application) your app knew what to do.
    and looked at the CLSID to see what object was going to be started. CLSID
    revealed that the object being referenced could be
    8E75D11-3D21-11d2-85C4-080009A0C626, thus start me now.
     
    Paul Richardson, Sep 20, 2004
    #3
  4. The hardest part about .NET is breaking old habits. In it, you have no need
    to use CreateObject or GetObject and shouldn't use them:

    Public Function Connect2ACAD() As Integer
    Try
    'get existing
    cadApp = GetActiveObject("AutoCAD.Application")
    Return 1
    Catch
    'no existing so try to create
    Try
    cadApp = New AcadApplication
    Return 2
    Catch
    'no AutoCAD found
    Return 0
    End Try
    End Try
    End Function

    This function creates or retrieves a session of AutoCAD and reports back
    whether it started it or not:

    0 = no AutoCAD found
    1 = current instance
    2 = started instance

    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Sep 20, 2004
    #4
  5. Thanks Mike, I was hoping someone would comment on the code...
     
    Paul Richardson, Sep 20, 2004
    #5
  6. Mike,

    I'd like to talk about an issue of style. Do your often use return codes
    like this?

    I avoid them. At the very least, where a value represents some form of
    enumeration, I create an enum for that purpose.

    However, in a situation like this I always opt to throw an exception
    with a descriptive message.
     
    Frank Oquendo, Sep 20, 2004
    #6
  7. Hi Frank,
    Well, often is a relative term =) In the half dozen or so projects I've
    done in .NET, yes, I've used them exclusively.
    I typically start Enums after 3 values. To me, anything less than 4 isn't
    worth the effort or should I say I don't see the value. I could be
    completely off base here, I'll admit that.
    Throw an exception to whom? A user or your calling app? I should mention
    that usually my apps don't involve users - especially the .NET ones. They
    are all automated "configurators" where the user enters data somewhere else
    and I receive notification from a web method, dbase entry, a dropped file
    in a watched directory, or passed as a parameter. My program then runs and
    passes back a return value that indicates success or failure as well as
    kicking information into a log file of some type. If I was creating a
    userinterface program, then I concur and would do as you suggest.

    In this particular case, the only reason I distinguish between Create and
    Get is so I know whether to shutdown AutoCAD or not. I actually raise an
    event error with the calling app that this snippet came from - which I
    stripped before posting because it would've confused things I thought.

    On the whole, I have read quite a few articles on using the Return as part
    of C# and VB.NET [all non-AutoCAD specific articles] and decided to make
    the habit of doing it that way. I'm far from a .NET expert so there may be
    underlying issues that I haven't stumbled across yet but this method seemed
    to make sense to me. So far I haven't had any problems with it either --->
    unless its the root of that weird dbase issue I had?! I'm open for any
    critiques or opinions on how best to incorporate .NET and Adesk stuff.

    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Sep 20, 2004
    #7
  8. I use enums simply for their declarative value. It avoids having to look
    through source or documentation to discover the meaning of a numeric value.

    For example: AutoCADSession.SessionCreated is a whole lot more readable
    than 2.

    When you're talking about numeric input from calling apps, there's just
    no other way to go as enums restrict the callers to valid input.
    The caller, of course. The caller can then choose how to deal with the
    problem. Not finding AutoCAD on the system is definitely an exceptional
    situation for an AutoCAD add-in so the behavior is quite appropriate in
    the instance you illustrated.
    I wrote a handly little class once that automatically connects to
    AutoCAD and uses an internal member to track whether it connected to or
    created an AutoCAD instance.

    That allowed the class to respond appropriately when its Dispose method
    was called. I'll see if I can dig it up.
    That's just too Win32 API for my taste. But as I mentioned earlier, it's
    just a matter of taste so it's not like there's any compelling reason
    to do things differently if your current practices serve you well.
     
    Frank Oquendo, Sep 20, 2004
    #8
  9. Here it is:

    http://server73.totalchoicehosting.com/~acadxfo/dotnet/001.htm

    The information dates back to AutoCAD 2000 but it ports just fine.
     
    Frank Oquendo, Sep 20, 2004
    #9
  10. Thanks Frank! I'm looking forward to reading it =)
    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Sep 20, 2004
    #10
  11. When you're talking about numeric input from calling apps, there's just
    Hmmmmmm..........I hadn't "looked" at it that way before only from the "got
    to code it" viewpoint. I'll have to reconsider my approach.
    How do you normally choose to handle it? Send back a value the conventional
    way and continue with the func terminating, etc. as you go? I was under the
    impression that the Return better handled clearing out vars and was some
    how associated with garbage collection. I'm very interested - you already
    turned me on the Enums =)

    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Sep 20, 2004
    #11
  12. Enums are great for this as well as bit-coded values.
    I've been doing so many classes that I normally implement some kind of
    LastError property for non-exceptional errors. The few times that I do
    return a value as a status indicator, I'll go with an enum (like the
    DialogResult return value from ShowDialog).
     
    Frank Oquendo, Sep 20, 2004
    #12
  13. Paul Richardson

    Tim Riley Guest

    Very nice article but I can't get it to print correctly from either Firefox
    or IE. It keeps cutting off the right side. Any ideas?

    ~Tim Riley
     
    Tim Riley, Sep 21, 2004
    #13
  14. Paul Richardson

    Tim Riley Guest

    I guess I could do that but it seems strange that it won't print correctly
    from a web browser. Thanks.

    ~Tim Riley
     
    Tim Riley, Sep 21, 2004
    #14
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.