Jump to content
barakar42

VBA AutoCAD command line variables

Recommended Posts

barakar42

Hi,

 

I'm currently writing a small program using VBA . The section of the program i'm working on is to modify the drawing borders. I found a Lisp file online that works wonders by finding and replacing a variable, but i can't seem to get it to work by using the Send Command

 

The lisp requires that you type in what you want to replace and then what to replace it with.

 

I cant seem to find a way to pass a variable to the command line so that it will replace what has been input

 

Here's my sample code:

---------------------------------------------------------------

Sub RENAME_BORDER()

 

On Error Resume Next
Dim acadCmd As String
Dim client As String
Dim location As String

 

client = Sheets(1).Range("N" & 20).Value
location = Sheets(1).Range("N" & 22).Value

 

Set ACAD = GetObject(, "AutoCAD.Application")
ACAD.Visible = True


acadCmd = "coa & ######### & client"

 

ACAD.ActiveDocument.SendCommand acadCmd & vbCr


End Sub

 

-----------------------------------

 

When you try that code, it runs the command, tells the lisp program to search for ########, but then i need to use a variable, such as cell values in N20 or N22 to replace all those #'s with .

 

Has anyone got any suggestions?

Share this post


Link to post
Share on other sites
PeterPan9720

Hi, 

I don't understand very well what do you mean, but if you are trying to modify a sort of "company template", you can ease create a block with attributes, (CUSTOMER, JOB NUMBER Release, and so on and modify it with VBA for sure. See attached Jpg image

CompanyFrame_With Blocks Attributes.JPG

Share this post


Link to post
Share on other sites
BIGAL

Looks like your running this from excel. So reading a cell range. I agree edit block attributes much easier can do it via attribute order or by att tagname. Also can control which layout tab etc to update.

Share this post


Link to post
Share on other sites
barakar42

Apologies for not being clear.

 

I've attached a screenshot of our border and the lisp program that i'm using

 

In the lisp program, i've specified what block to be looking at, our borders.

 

So to run the command you'll type "coa"

then it will ask for text that you want to replace

and the it will ask you for the new text that you want.

 

Its the final part that i cant get working.  VBA will send the command, the #'s but i then can't get e cell value to be put into the command line to change the #'s

 

All the text that i'll will be replacing will have a varying number of #'s in and they will be the same across up to 60 drawings. 

cad drawing border.png

fr.lsp

Share this post


Link to post
Share on other sites
PeterPan9720

Hi Thank you for explanation, I guess that with a Block with all information as attributes inside you can solve very fine your issue.

You can create a VBA form, where it will be required the text to be change, after this step procedure will search inside the drawing the drawing information title block, search attributes (text) to be change, and you solve your problem.

Blocks attributes could be changed also by hand with double click directly on block inside drawing and you can change the attributes containing the text to be change.

Send me an empty drawing I'll try to explain you better what I mean, than you can decide.

 

Regards

Share this post


Link to post
Share on other sites
barakar42

Thanks for your reply, 

 

This is only going to be a small part of a slightly larger program that me and another person are working on.

 

It's hard to try and explain it all, but basically an engineer wants to select a number of drawings from a list, click a button and it will copy the file to the relevant job folder (already completed),  sort out the drawings (already completed) and update all the borders (wip) with the relevant client, location, job number and date

 

The engineer in charge of this little project is adamant that he wants the borders doing despite it only taking a minute using the built in find and replace function of AutoCAD,. but there is no way of using that function through VBA which is why i'm trying to use the lisp. 

 

If you can think of another way of doing it then i'm all ears 

 

ive attached the file with the borders on 

CXXXX-E-000.dwg

Share this post


Link to post
Share on other sites
PeterPan9720

Hi barakar42

Seems that you already have title block, so half of job it's already done.

Just another couple of questions:

1) "select a number of drawings from a list" List could be a text file containing drawing list ? List is currently inside an excel file ? I'm asking this because inside procedure you can show a  "combobox" with file list drawing to apply, so source could be managed easy, in addition you can avoid to assign double time the same "drawing number" to different drawings.

 

2) I saw several "layout" or "Paper Space" in your drawing do you want to assign the same layout name to drawing inside ?.

 

I'll prepare a simple VBA code so to show you how to solve quickly your issue.

 

Bye

Share this post


Link to post
Share on other sites
barakar42

With regards to point 1. There's a list of every possible drawing , each with their own unique drawing title and number that the engineer just has to choose from, using a tick box next to each drawing number. There's no possibility of duplicating drawings

 

2. We only have 1 dwg file that has multiple layout tabs on, and everything drawn in model space. The borders are on paper space with viewports that show the correct drawing.

 

It's only the hashtags that need replacing with certain values that'll be on the excel sheet and they'll be the same on every layout tab.

Share this post


Link to post
Share on other sites
PeterPan9720

Hi barakar42,

here below some partial code to solve your problem

The below code has been written inside Excel Development Area.

As second issue I saw that you you have some duplicated "TITLE BLOCK" attributes, like as drawing number inside the left upper corner. This is not a good Idea, you can manage easy the drawing number indication in the upper left corner as a new attribute, that will be the same drawing number placed in the lower right drawing part.

The same for ISSUE Attribute

How you can manage the drawing:

For example:

You have a drawing list, with excel, you can set before the LAYOUT where do you want to apply excel information about cliente and so on.

You can add to Excel command bar a button recalling the below macro which will transfer information on you cad drawing.

The variable Attributelist it's an array containing all attributes TAG, PROMPT and actual VALUE.

Each TAG it's inside array with properties see pictures below:

image.png.c229bd8cf9c0fcfcd9b95a1ac4cd1c08.png

Attribute array 0

image.png.a760dc6e77442b086cc886aa96b48517.png

SO you will have a TAGSTRING which is the Attribute TAG and TEXTSTRING which is the value.

In this way you can modify the value with AttributeList(0).TextString= "EXCEL ROW or CELL VALUE". the same for the others attributes (CLIENT or DWG NUMBER)

The above sentence could be added where indicated  '  '' <do something with this block here >" 

Hi hope that could be help you more.

If you need more clarificatios let me know

Sub RENAME_BORDER()

Dim client As String
Dim location As String
Dim acadApp As Object
Dim acadDoc As Object
Dim MyMag As Double

client = Sheets(1).Range("N" & 20).Value
location = Sheets(1).Range("N" & 22).Value
Bname = "TITLE BLOCK"
    On Error Resume Next

 'Check if AutoCAD application is open. If is not opened create a new instance and make it visible.
    Set acadApp = GetObject(, "AutoCAD.Application")
    Set acadDoc = acadApp.ActiveDocument
   If acadApp Is Nothing Then
       Set acadApp = CreateObject("AutoCAD.Application")
        acadApp.Visible = True
   End If
        acadApp.Visible = True
        
    
    Set NewoBlock = acadDoc.ActiveLayout.Block
        For iCount = 0 To NewoBlock.Count - 1
            Set oEnt = NewoBlock.Item(iCount)
                If TypeOf oEnt Is AcadBlockReference Then
                    Set NewoblkRef = oEnt
                        If UCase(NewoblkRef.Name) = UCase(Bname) Then
                           If NewoblkRef.HasAttributes = True Then
                                AttributeList = NewoblkRef.GetAttributes
                '  '' <do something with this block here >
                                
                                Exit For
                           Else
                                bCount = bCount + 1
                           End If
                
            End If
        End If
    Next iCount

End Sub

 

Share this post


Link to post
Share on other sites
barakar42

Hi Peter, 

 

Thanks for the code. I'm having a couple issues getting it working though

 

Does it work by having to have all possible attributes set up for all possible blocks first, and then goes through and check and chagnes the values? 

Share this post


Link to post
Share on other sites
PeterPan9720

Yes it's true, whenever procedure ill find the blocks "TITLE BLOCK" you can get attributes and modify as per above AttributeList(0).TextString= "EXCEL ROW or CELL VALUE", or other attributes that you need with AttributeList(1).Textsring="EXCEL ROW COLUMN", and more.

Modifying the duplicated attributes, you can assign by above procedure the value for drawing number (just one of duplicated attributes) on both places as you need, because you will assign the same excel row or column contents to two different block attribute, AttributeList(XX).Textsring="EXCEL ROW COLUMN FOR DRAWING NUMBER", AttributeList(YY).Textsring="EXCEL ROW COLUMN FOR DRAWING NUMBER SECOND POSITION",  the same for ISSUE Attribute.

On the opposite there is a way to select specific block with selection set, but due to that amount of blocks inside paper space are not thousand the procedure execution time it will be the same. Try it and let me know, or on the opposite you can give me also Excel File I'll try to write it for you.

Regards

Edited by PeterPan9720

Share this post


Link to post
Share on other sites
barakar42

i'm trying to run the code and it comes up with the error:

 

user defined type not defined on line "TypeOf oEnt Is AcadBlockReference"

 

 

Share this post


Link to post
Share on other sites
PeterPan9720
9 minutes ago, barakar42 said:

i'm trying to run the code and it comes up with the error:

 

user defined type not defined on line "TypeOf oEnt Is AcadBlockReference"

 

 

 

First Question are you using Autocad or similar CAD ?

and second issue please check the Option selected on Tools reference on Excel Development Area see picture

image.png.056eaefa5c13b70f83fdb78db50d1bdd.png

Edited by PeterPan9720

Share this post


Link to post
Share on other sites
barakar42

Where does the attribute list get saved so i can check which attributes i need to change?

 

Also, is it possible to only change part of an attribute, e.g

 

C####-E-000 to C1234-E-000

Share this post


Link to post
Share on other sites
PeterPan9720
33 minutes ago, barakar42 said:

Where does the attribute list get saved so i can check which attributes i need to change?

 

Also, is it possible to only change part of an attribute, e.g

 

C####-E-000 to C1234-E-000

All attributes are stored in AttributeList array, during routine debug you can check which is the attributes you need to change see picture above, check the properties AttributeList(XX).TAGSTRING for information on NAME of Attribute and AttributeList(XX).TEXSTRING for modification.

For modification you can insert:

 

If AttributeList(6).TEXTSTRING="C####-E-000" then ' DRAWINGNO SHALL BE at position 6 of AttributeList array check with debug.

    AttributeList(6).TEXTSTRING="C1234-E-000" 

end if

image.png.02d6d91f34dbd944b94cb01d3bb982da.png

Of course Attribute value it's a string so you can manipulate as you need, it's could be from excel cell value, or LEFT("XXX", CharPos) or MID, or RIGHT.

 

I hope above could solve your issue.

 

Edited by PeterPan9720

Share this post


Link to post
Share on other sites
barakar42

I understand that you have to run the program and "watch" the attributes to see the full list of them all, but when i try , it doesn't appear in a list like what you get. I'm probably doing something wrong, i've not got much experience with VBA yet

Edited by barakar42

Share this post


Link to post
Share on other sites
PeterPan9720
16 minutes ago, barakar42 said:

I understand that you have to run the program and "watch" the attributes to see the full list of them all, but when i try , it doesn't appear in a list like what you get. I'm probably doing something wrong, i've not got much experience with VBA yet

Ok let explain goto into editing of code on row  

AttributeList = NewoblkRef.GetAttributes

and type function key F9 so you insert a break (you will see a red row) of procedure when it is running and will pass on that point of code. 

Run the procedure and when break point will be reached click with right mouse button and select add watch option. 

On VBA software you will have a debug windows where you can see the "watched" variable. Click on the + sign on visible variable on that window and you will have all properties of selected variable (see picture on previous post). 

Search the attributes name in TAGSTRING property value, and then you can modify the TEXTSTRING property value for change it as I showed you before. 

Share this post


Link to post
Share on other sites
barakar42

I've followed those steps, but on the watch list, it says value is empty and there's no option for the plus icon 

BREAK POINT.png

Share this post


Link to post
Share on other sites
PeterPan9720
6 minutes ago, barakar42 said:

I've followed those steps, but on the watch list, it says value is empty and there's no option for the plus icon 

BREAK POINT.png

Let the procedure execute the code in row because when you break on a specific row this will be not executed. 

Press F8 function key and watch again. And press F5 to run again until next breakpoint will be reached again. 

 

Edited by PeterPan9720

Share this post


Link to post
Share on other sites
barakar42

Thanks Peter, I can see that now. I'm going to try and edit the values now.

 

Your help has been invaluable! 😀

Share this post


Link to post
Share on other sites

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
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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