How to start autocad from VB (not VBA)

Discussion in 'AutoCAD' started by Paul-de-Boer, Jan 27, 2005.

  1. Paul-de-Boer

    Paul-de-Boer Guest

    Is it possible to start autocad from VB open a drawing and switch to one of the layouts saves within the drawing.

    Thanks
    P. de Boer
     
    Paul-de-Boer, Jan 27, 2005
    #1
  2. Paul-de-Boer

    VBA Guest

    Sure thing, here's how it's done.


    Dim oAcadApp As AcadApplication

    If LoadAutoCAD(oAcadApp) Then
    With oAcadApp
    .ActiveDocument.ActiveLayout =
    oAcadApp.ActiveDocument.Layouts("Layout1")
    End With
    End If


    Public Function LoadAutoCAD(oApp As AcadApplication) As Boolean
    On Error Resume Next

    'try to get running instant of AutoCAD
    Set oApp = GetObject(, "AutoCAD.Application")

    If Err Then
    Err.Clear

    Set oApp = CreateObject("AutoCAD.Application")

    'exit function, no AutoCAD available
    If Err Then Exit Function
    End If

    'AutoCAD instant open, make it visible
    oApp.Visible = True
    'return True that AutoCAD was opened successfully
    LoadAutoCAD = True
    End Function



    of the layouts saves within the drawing.
     
    VBA, Jan 27, 2005
    #2
  3. VBA,
    What is this LoadAutoCAD() function?
    Kenneth Hutson
    San Antonio, TX
     
    Kenneth Hutson, Jan 27, 2005
    #3
  4. Paul-de-Boer

    VBA Guest

    It's with the code I posted, look down the page.
     
    VBA, Jan 27, 2005
    #4
  5. I noticed it never returns False.
     
    Frank Oquendo, Jan 27, 2005
    #5
  6. Paul-de-Boer

    VBA Guest

    It sure does, all functions return False by default. If I didn't have the
    LoadAutoCAD = True statement in there it would return False, hence the
    statement.
     
    VBA, Jan 27, 2005
    #6
  7. I can't say it's a good idea to rely on such things. It's also not at
    all obvious. Why not just actually return False rather than exiting the
    function?
     
    Frank Oquendo, Jan 27, 2005
    #7
  8. Paul-de-Boer

    VBA Guest

    It's been this way since the beginning of VB. It's not like C/C++. I have
    done it this way for years and years and I have never had any reason even
    remotely requiring otherwise.
     
    VBA, Jan 27, 2005
    #8
  9. You're not interested in self-documenting code? Very well then.
     
    Frank Oquendo, Jan 27, 2005
    #9
  10. Paul-de-Boer

    VBA Guest

    It's really pretty self-explanatory. It's standard VB language usage, not
    rocket science.
     
    VBA, Jan 27, 2005
    #10
  11. Obviously not. I'm not trying to pick a fight or put you down. I'm just
    pointing out that your code requires the reader to understand a feature
    of the language when it could just as easily make itself plainly evident:

    If Err Then LoadAutoCAD = False

    That's it. I've been working with VB since version 3 and have never
    heard that a function returns False by default. Even now, I'm sure
    that's an oversimplification. It's far more likely functions return the
    default value for their return type if exited without an explicit return
    value.
     
    Frank Oquendo, Jan 27, 2005
    #11
  12. BTW, far from trying to imply you may be incorrect, I was pointing out
    that I honestly did not know about default return values despite my
    experience with the language.

    It's reasonable to assume others may not be aware of that feature as well.
     
    Frank Oquendo, Jan 27, 2005
    #12
  13. Paul-de-Boer

    VBA Guest

    Actually, I did misspeak a bit when I said all functions.

    I should have said all functions whos return datatype is Boolean.

    It's the same principle as when you are declaring variables.

    Dim X As Boolean

    X's initial value is set to False.

    That's why I don't understand why it's such a big deal to leave it out of
    the function.
     
    VBA, Jan 27, 2005
    #13
  14. That's why I don't understand why it's such a big deal to leave it out of
    FWIW following a self documented regime would require you to set the
    function to false instead of leaving it implied. For example, if you leave
    your company and your replacement opens your code, he may not realize that
    the function returns false because the VB documentation does not explicitly
    state that. Been down that road with clients that purchase source code, so
    now its second nature to add it.

    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Jan 27, 2005
    #14
  15. Paul-de-Boer

    VBA Guest

    From the VB help files

    To return a value from a function, assign the value to the function name.
    Any number of such assignments can appear anywhere within the procedure. If
    no value is assigned to name, the procedure returns a default value: a
    numeric function returns 0, a string function returns a zero-length string
    (""), and a Variant function returns Empty. A function that returns an
    object reference returns Nothing if no object reference is assigned to name
    (using Set) within the Function.
     
    VBA, Jan 27, 2005
    #15
  16. Hi,

    I speak from having knowledge of the features since the VB1 era, but still
    use the set to false my code by habit and for self-documentation.

    Clearly it is a personal thing.

    --

    Regards,


    Laurie Comerford
    www.cadapps.com.au
     
    Laurie Comerford, Jan 27, 2005
    #16
  17. Paul-de-Boer

    LochDhu Guest

    I personally do the same as VBA. For those of you who don't, Mikey, do you
    do this?

    Dim i as integer
    i = 0
    for i = 0 to somecollection.count
    'blah
    next


    Scott
     
    LochDhu, Jan 27, 2005
    #17
  18. Paul-de-Boer

    LochDhu Guest

    ....and just so you don't nit pick...

    for i = 0 to Ubound(somearray)

    but you get the idea....
     
    LochDhu, Jan 27, 2005
    #18
  19. That's a waste of time considering you're going to set the value in your
    very next line of code.
    I used to do that all the time. It's inefficient as the property must be
    queried with each iteration. Now I do this:

    ' When knowing an item index is handy
    Dim min As Long, max As Long, i As Long
    min = LBound(array)' or 1 for collections
    max = UBound(array)' or <collection>.Count
    For i = min To max
    ' Do your thing
    Next

    Or this:

    'When an index is not desired.
    For Each <objectType> In <collection>
    ' Do your thing
    Next

    BTW, VB arrays support iteration so long as your enumeration variable is
    of type Variant.
     
    Frank Oquendo, Jan 28, 2005
    #19
  20. Paul-de-Boer

    LochDhu Guest

    Frank,

    I understand and agree with your thoughts, but the idea was more simplistic.
    I should have stated:

    Dim i as integer
    i = 0
    i = 10 + 2

    Believe it or not, I see this in a lot of code.

    Getting back to the original thread - if you initialize a variable as
    boolean datatype that variable has a value - false, we all know that. A
    function is no different - as boolean = false. There's no need to
    explicitly set it = false. I see so much code where a function is defined
    as a boolean and the very FIRST line of code is 'FunctionName'= False.

    So the point is - if you initialize a var as boolean it = false. why set it
    to false???

    I'm just advocating VBA's approach to this. As I have done this for years
    without issues. If a programmer looks at my code in 2 years doesn't
    understand what is being returned then he needs some training.

    Scott
     
    LochDhu, Jan 28, 2005
    #20
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.