Jump to content

Inserting blocks via VBA Buttons/ check boxes


Recommended Posts

Posted

Hi Forum,

 

I'm Totally new to Visual Basic Programming and i just had a few questions;

 

does VBA start up via LISP command? or some other way?

 

i'm trying to insert 6 blocks that i constantly use into my drawings. I would like to be able to check which blocks i want and then click 'OK' and the blocks insert to 0,0,0. I currently have all my blocks in a master file.

 

being new to VBA i have no idea where to start. does anyone have anything similar i can work with?

 

I Have attached what my menu will look like.

 

Cheers guys, i appreciate all your help. :)

menu.jpg

Posted

If you are totally new I would recommend looking at VB.Net rather than VBA. As you should already have found, you need to download a VBA enabler to get it to work with newer versions of AutoCAD. You can download Visual Basic Express from the Microsoft site for free that will aloow you to write code in VB.Net. Once that is done you will still need to download some files to get access to the AutoCAD bits. Once you have written your routines you compiled them to a dll that can be loaded into AutoCAD.

 

One way to run a VBA routine is to issue the VBASTMT command and then your routine call. I have my routines saved within the ACAD.dvb file (so that it is loaded automatically) and then have a button with (something like) ^C^Cvbastmt;cloud; to run the "cloud" routine.

 

Having said that you should learn VB.Net rather than VBA I find VBA easier - although it must be admitted that I have only just started tackling VB.Net seriously this year. My boss bought me Jerry Winters' book for christmas (not literally) and it has been a great help at getting started. I quickly tired of its presentation and content but I wouldn't have got started without it.

Posted

Realising I didn't actually answer your question, here is an extract of one of my VBA routines that inserts a block. For this to work the block (TRI) must be in a pathed folder, not contained within another file. I don't know how to obtain a block from within another file using VBA but I know it can be done in VB.Net and LISP.

 

Public Sub CloseCloud()
Dim BlockRefObj As AcadBlockReference
Dim P1(0 To 2) As Double
Dim CurrentLayer(0 To 1) As Boolean
Dim TriPos As Variant
Dim CScale As Single
Dim layerColl As AcadLayers
Dim CloudLayer As AcadLayer
Dim Iss As String
Dim varAttributes As Variant
On Error GoTo 0
CurrentLayer(0) = ThisDrawing.ActiveLayer.LayerOn
CurrentLayer(1) = ThisDrawing.ActiveLayer.Lock
ThisDrawing.ActiveLayer.LayerOn = True
ThisDrawing.ActiveLayer.Lock = False
CScale = Val(TextBoxScale.Text)
If CScale = 0 Then CScale = 1

TriPos = ThisDrawing.Utility.GetPoint(, "Triangle position ")
If OptionButton1.Value = True Then
   Set BlockRefObj = ThisDrawing.PaperSpace.InsertBlock(TriPos, "Tri", CScale, CScale, CScale, 0)
   Else
    Set BlockRefObj = ThisDrawing.ModelSpace.InsertBlock(TriPos, "Tri", CScale, CScale, CScale, 0)
   End If
Iss = TextBoxIssue.Text
Set layerColl = ThisDrawing.Layers
Set CloudLayer = layerColl.Add("Cloud_" & Iss)
varAttributes = BlockRefObj.GetAttributes
If TextBoxIssue.Text <> "" Then varAttributes(0).TextString = TextBoxIssue.Text
BlockRefObj.Layer = CloudLayer.Name
ThisDrawing.ActiveLayer.LayerOn = CurrentLayer(0)
ThisDrawing.ActiveLayer.Lock = CurrentLayer(1)
ThisDrawing.Regen acActiveViewport

End Sub

This reads a value from an option button on my form (model space or paper space), the layer I want it on and the scale of the drawing from text boxes on the form. It then asks you to select where the block is to be positioned and finally enters one (of the two) attributes.

 

The code is quite old so may not be the way I would do it now. I will find out soon how I would write it now as it is the next routine to be converted to VB.Net.

Posted

You can in dcl have images appear as well you can also in VBA no doubt in .net also if your going to this much trouble I would look at that as the final result.

 

I have done various lisps for 2x2 3x2 4x4 etc panes and just use the version required passing a list of images which is filename done in lisp &dcl.

 

Old fashioned way with slides but similar for user definable versions.

 

blocks.JPG

Posted

Just thought of another way something thats hardly talked about any more you can do pop side menus these can have your drawing name in them like a pull down menu pick line block inserts, extra line is insert all last line is close side menu. This is a great method for allowing predefined values to be picked rather than typed. If you use notedpad then you can do a side menu.

 

Will add a image shortly.

Posted

following on from BigAl, the solution really depends on what you are trying for. If your main aim is to get the blocks into your drawing then it is very simple to drop them on to a palette. If your main aim is to learn how to program then look in to VB.Net or LISP.

Posted

Hi guys,

thank you for your quick replies.

 

I have decided to use vba, only because of the functionality, i like being able to design what my forms will look at the end of the day.

 

I will most likely end up using this 'block inserting' function for more than just the standard things i use.

 

dbroada, i tried using your code that you supplied earlier and it does not work, i have compiling errors for some reason.

 

is it possible for you to modify the code to incorporate the 6 buttons and six block inserts then repost? I think i will be more capable of modifying it.

 

do you have any recomendations of websites or links i would be able to reference to?

 

cheers guys,

 

i really appreciate all your help.

Posted

this should work:

Public Sub InsertBlock()
Dim BlockRefObj As AcadBlockReference
Dim P1(0 To 2) As Double
P1(0) = 0: P1(1) = 0: P1(2) = 0
If CheckBox1.Value Then Set BlockRefObj = ThisDrawing.ModelSpace.InsertBlock(P1, "Block1", 1, 1, 1, 0)
End Sub

you will obviously have to draw your form and ensure that Block1 is available. I have removed all the layer information as that may have been causing your problems.

Posted

*cough*

 

... Why not just use a Tool Palette? :unsure:

Posted
*cough*

 

... Why not just use a Tool Palette? :unsure:

you mean like I suggested several posts earlier?

 

certainly the easiest way to do what he is wanting, especially as the blocks are already contained in one drawing - not a trival task using VBA.

Posted

you mean like I suggested several posts earlier?

 

certainly the easiest way to do what he is wanting, especially as the blocks are already contained in one drawing - not a trival task using VBA.

 

I was more hoping to reiterate the point to the OP, and not suggesting that the suggestion had not been yet suggested. :P

 

I'm all for developing a given project for the purposes of learning, etc. (I do this all the time)... But when it comes to production efficiency I very much turn to the simplest solution (in this case a Tool Palette), so long as any inherent difference in functionality is not a critical sacrifice.

Posted

I'd rather not use a tool pallette as we already use it for all of our standard blocks and notes.

 

In the end i will be using the 'block inserter' vba code to create all of my legends for my pages... i guess you could call it a legend creator. I will be able to choose which line types i want to show in that dwg. and then they insert themselves one after the other. Well that is my main goal anyways.

 

I'm open to other suggestions if anyone has done something similar?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...