Consulting

Results 1 to 18 of 18

Thread: Solved: Coding for AutoCad

  1. #1
    VBAX Regular
    Joined
    Jun 2005
    Location
    Hiawatha Iowa
    Posts
    31
    Location

    Question Solved: Coding for AutoCad

    Hello all,
    im new to VBA, about 3 weeks. I have a couple of books and im diving in..
    im trying to write some code fore Autocad...i used autocad for 8 years and have written some lisp routines but nothing with dialog boxes, hence the VBA.

    vba is quite different than lisp or DOS so getting a grip on the concepts is a challenge....on to the question.

    heres my code so far:

    [VBA] Sub ITB()
    Load ITBmain
    If ThisDrawing.ActiveSpace = acModelSpace Then ThisDrawing.SendCommand "tilemode 0 "
    ITBmain.Show 'a form with alot of Option Buttons and a Command Button
    If ITBcontinue = True Then ThisDrawing.SendCommand "-layer m XR-TITLE "
    End Sub
    [/VBA]
    what happens is that it hangs in the code at the continue button and never gets back to create the layer: (command button code follows):

    [VBA] Private Sub ITBcontinue_Click()
    ITBcontinue = True
    ITBmain.Hide
    End Sub
    [/VBA]
    what am i doing wrong?
    Thank you in advance for any help you can provide.
    ccastelein

  2. #2
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Hi ccastelein,
    Welcome to VBAX!

    Change the name of ITBcontinue to something else. VBA is getting confused because the command button has that name. That will get your code to work.

    This is what I would have done, this is just my opion
    [VBA]
    Sub ITB()
    Dim mLyr As AcadLayer
    If ThisDrawing.ActiveSpace = acModelSpace Then ThisDrawing.SendCommand "tilemode 0 "
    ITBcontinue.Show 1 'means stop execution until form is unloaded
    'ThisDrawing.SendCommand "-layer m XR-TITLE "
    Set mLyr = ThisDrawing.Layers.Add("XR-TITLE")
    mLyr.Color = acCyan
    mLyr.Linetype = "CONTINUOUS"
    End Sub

    Private Sub ITBcontinue_Click()
    'ITBcontinue = True
    ITBcontinue.Hide
    End Sub

    [/VBA]

    Let me know if I can be of more help

  3. #3
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Hi ccastelein,
    just to reinforce the point about confusing names - if your just getting started, it's worth getting into the habit of using some kind of naming convention so you can recognise your VBA objects and variables for what they are as well as describing them e.g.
    frmITB UserForm
    cmdITB CommandButton
    txtITB TextBox
    optITB OptionButton
    blnITB Boolean variable
    strITB String variable
    etc... etc...

    Best of luck with your VBA studies - drop by anytime you have a question
    K :-)

  4. #4
    VBAX Regular
    Joined
    Jun 2005
    Location
    Hiawatha Iowa
    Posts
    31
    Location
    Tommy and Killian,

    Thanks very much for your replies....i will give it a try asap and let you know..

    ccastelein

  5. #5
    VBAX Regular
    Joined
    Jun 2005
    Location
    Hiawatha Iowa
    Posts
    31
    Location
    ok, im not understanding something... here is my new code:
    [VBA] Sub ITB()
    If ThisDrawing.ActiveSpace = acModelSpace Then ThisDrawing.SendCommand "tilemode 0 "
    ITBmain.Show 1
    If cmdITB = True Then GoTo Endoffile
    ThisDrawing.SendCommand "-layer m " & "XR-TITLE" & vbCr & vbCr
    If titleblock24x36 = False Then ThisDrawing.SendCommand "-insert " & "U:\VP-CAD\SUPPORT\TITLE BLOCKS\TB-24 x 36.dwg" & vbCr & "0,0,0 1 0 "
    Endoffile:
    End Sub

    '(BUTTON)
    Private Sub cmdITB_Click()
    cmdITB = True
    ITBmain.hide
    End Sub
    [/VBA]
    NOW...two condition statements are working in EXACTLY OPPOSITE of what i would expect. if the button is clicked, cmdITB is set to true.. my code checks to see if its true, if it is, its supposed to go to the end...but it doesnt it creates and sets my layer to XR-TITLE... THEN its checks to see if titleblock24x36 is false (which it isnt) and it inserts the block (which it shouldnt)....

    so heres the thing...if a radio (option) button is selected (value set to true), upon exiting from the form, will it be TRUE or FALSE....??? i thought if its selected it would be TRUE...(am i wrong). cause its working exactly oppisite.
    the above TRUE and FALSE i changed and got the results i wanted. I would normally reverse the statements to be FALSE and TRUE
    what and why? im going crazy!
    thanks for any help..
    ccastelein

  6. #6
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Hi ccastelein,
    The var cmdITB is being used as a global var. In reality though it is a local var in 2 areas.
    Like
    (defun C:A (/ cmdITB) cmdITB is local only
    (setvar cmdITB True)
    )
    (defun C:A () cmdITB is Global only
    (setvar cmdITB True)
    )

    my syntax is most likely off it's been a while since I've done lisp

    The below VBA should fix your problem. I personally like to dim all my vars This lets me know what the value is up front so I can code without assumtions.

    [VBA]
    Public cmdITB As Boolean
    Sub ITB()
    Dim titleblock24x36 As Boolean
    If ThisDrawing.ActiveSpace = acModelSpace Then ThisDrawing.SendCommand "tilemode 0 "
    ITBmain.Show 1
    If cmdITB Then GoTo Endoffile
    titleblock24x36 = True
    ThisDrawing.SendCommand "-layer m " & "XR-TITLE" & vbCr & vbCr
    'titleblock24x36 is empty which evaluates to false
    If titleblock24x36 = False Then ThisDrawing.SendCommand "-insert " & "U:\VP-CAD\SUPPORT\TITLE BLOCKS\TB-24 x 36.dwg" & vbCr & "0,0,0 1 0 "
    Endoffile:
    End Sub

    Private Sub cmdITB_Click()
    cmdITB = True
    ITBmain.hide
    End Sub
    [/VBA]

    Still willing to help you through this

    Tommy

  7. #7
    VBAX Regular
    Joined
    Jun 2005
    Location
    Hiawatha Iowa
    Posts
    31
    Location
    Hey Tommy,

    Pardon me if im just not getting it...im trying. Heres the question...if i have a form with different groups of radio buttons...and in each group there a is a radio button marked TRUE with no other code showing...
    Private Sub titleblock24x36_Click()
    End Sub
    shouldnt i be able to just evaluate the button name for true or false, and shouldnt that button be TRUE if i set it to TRUE, when i the continue button is pressed??
    if titleblock24x36 = true then del c:\*.* (joke)
    i guess this is where it comes down to it....what good is the button name if it dont hold a value?

  8. #8
    VBAX Regular
    Joined
    Jun 2005
    Location
    Hiawatha Iowa
    Posts
    31
    Location
    Ok, i put a msgbox inside the _click () and in the module that echos the button vaule...while in the form it (when clicked on) it echos true. once hidden it echos false...sooo... somehow it forgets its default value upon exiting from the form... is there a way to make it remember this, or at least evaluate its settings upon exit? so value conditions can be made? am i on the wrong track? or should i make up variables independent of the button name...but then i somehow need it to update the values on exit...im lost.
    ccastelein

  9. #9
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Hi I was in meeting Sorry I will get back in a couple hours for further discussion.

    The reason it is loosing the default value is it is not a global variable it is a local (which means the variable is not available to anything after exiting the sub or function)

  10. #10
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Hi ccastelein,

    I can go into more detail now.
    This is my terminalogy :
    Local variable : variable the is dim/used in a sub or function once this sub or function is through executing it is no longer available. If a variable is defined as Dim/Private in a module or form it is avaliable to all functions/subs in that form or module only.

    Global variable : Is defined as "Public titleblock24x36 as Boolean" in a module it is available to all functions and subs.

    When the form is unloaded (unload me) all local variables are destroyed that are used in that form. If a variable is defined public in a module, it can be accessed by all functions/subs and retain its value throughout the program. DO NOT use Public to inialize a variable in a form, this will cause a lot of problems, you cannot unload the form from memory (not good). The biggest problem I see you are having is scope, once the form is unloaded it goes out of scope (nothing there now).

    HTH
    Tommy

  11. #11
    VBAX Regular
    Joined
    Jun 2005
    Location
    Hiawatha Iowa
    Posts
    31
    Location

    think i got a solution

    ok tommy,
    you got me thinking...i assumed (my bad) that once a form was left that the values of the buttons were kept and could be evaluated in the module...and that was the second assumtion..that i had to do the evaluating in the module..
    it struck me when you said that it wasnt a global value, that i should do the evaluating when the "coninue" button is clicked and then leave the form..
    im not trying to write a real complicated vba routine..just one that inserts a titleblock with the size selected by the user..

    from my preliminary tests i should be able to get what i want fairly easy. I will post back tomorrow when i get back to work and bang it out..
    Thank you for hanging in there with the newbies...we all have to start somewhere.

    speaking of which, can you recommend a vba book that is really good at explaining the whole vba environment... i have two books..
    one is so simplistic that its really only good for making forms....the next is from autodesk and gets WAY out there to fast for me to follow.

    again thanks! ill post back.
    ccastelein

  12. #12
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    LOL you were thinking like in lisp with the dcl forms? I don't know of any vba books I learned this on my own (I sure you or anyone else can tell).
    One thing that helped me a lot is my viewpoint. Once I figure out the scope, and almost everything in vba has properties and methods, it got easier. Then I found the object browser, that help me find the properties and methods to get done what I wanted. And of course there is always the help. I cut and paste samples from the help and step through it till I understand, modifing just to see what happens, basically just trial and error.
    I'll be here to help when and where I can

  13. #13
    VBAX Regular
    Joined
    Jun 2005
    Location
    Hiawatha Iowa
    Posts
    31
    Location
    ok, its coming along but im running into a problem that shouldnt be a big deal, but i cant seem to get it right...

    In autocad im in a drawing "xxx.dwg" and im opening another drawing with: AcadDocument.Open XRpath (where XRpath is the path and name of the drawing i want to open)

    then i want to issue commands to this drawing...BUT when i open that drawing it doesnt become the active window so my issued commands are being issued in the xxx.dwg and not me new one....
    Any ideas how to get the new drawing "xr-title.dwg" the acitve drawing?
    ccastelein

  14. #14
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Hi ccastelein,

    I personally prefer to work on 1 drawing at a time. That way I don't confuse myself so much

    Dim MyDwg As AcadDocument
    Dim A As AcadDocument
    A.Close True 'this command will close the current drawing saving the changes.
    Set MyDwg = ThisDrawing.Application.Documents.Open XRpath

  15. #15
    VBAX Regular
    Joined
    Jun 2005
    Location
    Hiawatha Iowa
    Posts
    31
    Location
    surprisingly the actual solution was alot simpler:
    tempdwg.Activate

    but im having another problem... with the following two lines of code it wont copy a dwg to a new location...
    Set newXR = CreateObject("AutoCAD.document")
    newXR.CopyFile "U:\VP-CAD\SUPPORT\OFFICE LOGOS\XR-Title.dwg", projectpath

    it gives me the error "active x component cant create object"...ive changed "AutoCAD.document" to several different things...but cant seem to get the object type correct.
    "acadapplication.application" etc.
    i dont actually have to create an object i just want to copy the file to another location...do i need the SET command at all in this? if not what do i replace "newXR" with?
    much confused..
    ccastelein

  16. #16
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi ccastelein
    I've retitled your question to relate to the subject.
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  17. #17
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Hi again ccastelein,

    I see no Document.CopyFile in Acad 2000i. Why not just copy the file, if that is what you are trying to do.

    FileCopy "U:\VP-CAD\SUPPORT\OFFICE LOGOS\XR-Title.dwg", projectpath & "XR-Title.dwg"

    I have no idea what is actually in the var projectpath so if there is no "\" you will need to add it.

  18. #18
    VBAX Regular
    Joined
    Jun 2005
    Location
    Hiawatha Iowa
    Posts
    31
    Location
    Thats it!....
    Well, thanks tommy you have helped my write my very first VBA routine...
    and it works wonderful!
    its nothing fancy but everything starts that way...
    Thanks again!
    im sure ill be back... i think ive got the bug now.
    ccastelein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •