PDA

View Full Version : Solved: Coding for AutoCad



ccastelein
06-02-2005, 07:35 PM
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:

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

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):

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

what am i doing wrong?:think:
Thank you in advance for any help you can provide.
ccastelein

Tommy
06-03-2005, 07:11 AM
Hi ccastelein, :hi:
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 :yes

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



Let me know if I can be of more help :friends:

Killian
06-03-2005, 07:48 AM
Hi ccastelein, :hi:
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 :thumb

ccastelein
06-04-2005, 07:19 AM
Tommy and Killian,

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

ccastelein

ccastelein
06-06-2005, 11:22 AM
ok, im not understanding something... here is my new code:
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

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.:doh:
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

Tommy
06-06-2005, 11:59 AM
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.


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


Still willing to help you through this :)

Tommy

ccastelein
06-06-2005, 01:26 PM
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?

ccastelein
06-06-2005, 01:55 PM
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

Tommy
06-06-2005, 02:33 PM
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)

Tommy
06-07-2005, 10:40 AM
Hi ccastelein, :hi:

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

ccastelein
06-07-2005, 08:09 PM
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! :bow: ill post back.:hi:
ccastelein

Tommy
06-08-2005, 06:13 AM
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 :)

ccastelein
06-14-2005, 07:11 AM
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

Tommy
06-14-2005, 07:41 AM
Hi ccastelein,

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

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

ccastelein
06-20-2005, 11:08 AM
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

mdmackillop
06-23-2005, 03:18 PM
Hi ccastelein
I've retitled your question to relate to the subject.
Regards
MD

Tommy
06-24-2005, 06:21 AM
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.

ccastelein
06-25-2005, 09:46 PM
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