PDA

View Full Version : ACAD VBA to insert drawing?



JHammond53
12-21-2006, 09:49 AM
Is it possible to have a VBA that will insert a drawing that has our basic layers everytime we open up a new/existing project? I read the thread about the VBA that created or rewrote existing layers, but I would think it would be easier to just have our layers inserted using a .dwg file.

EDIT: I am totally dumb-founded when it comes to VBA...it looks like a foreign language to me.

lucas
12-21-2006, 10:48 AM
Hi JHammond,
I'm wondering why you don't just create a template out of your existing drwg. Just delete everything on the model view...delete all of your paper space tabs except one new blank one and go to file-save as template and name it accordingly. Then you can set up cad to use that template every time you create a new drawing...

fixo
12-21-2006, 11:00 AM
Is it possible to have a VBA that will insert a drawing that has our basic layers everytime we open up a new/existing project? I read the thread about the VBA that created or rewrote existing layers, but I would think it would be easier to just have our layers inserted using a .dwg file.

EDIT: I am totally dumb-founded when it comes to VBA...it looks like a foreign language to me.

Try to doing the following one:
Open drawing you want to copy layers from, do not close it
and then open a new drawing
An openig order is important!
Open VBA Editor , insert module: menu Insert->Module
Put my code into this module
You can also to save this module with name other then Module1
(i.e. rename it to modCopyLayers) then go to menu File->Export file and save this
module in the desired folder
Then you can to go menu Run->Run macro
When you will be close drawing, you can to change this
as the dvb project,
press "Yes" button on close and save project with name
you want
Sorry for my poor explanation
Hth


Option Explicit
Sub CopyLayersToNew()
Dim sourceDWG As AcadDocument
Dim targetDWG As AcadDocument
Dim oLayer As AcadLayer
Dim i As Integer
' get first opened document in documents collection
Set sourceDWG = Application.Documents.Item(0)
' get second one (new)
Set targetDWG = Application.Documents.Item(1)
' create array of layers
Dim copyVar() As Object
' loop through layers collection in source dwg
For Each oLayer In sourceDWG.Layers
' exclude these layers :
If Not oLayer.Name Like "0,Defpoints" Then
' redimensioning an array
ReDim Preserve copyVar(i)
' add next layer object to an array
Set copyVar(i) = oLayer
' increment a counter
i = i + 1
End If
Next
' dummy argument (optional, see Help)
Dim idPairs As Variant
' create array of variants
Dim copyObj As Variant
' copying layers
copyObj = sourceDWG.CopyObjects(copyVar, targetDWG.Layers, idPairs)
' close source drawing without of saving changes
sourceDWG.Close savechanges:=False
' clean up memory, release a document object
Set sourceDWG = Nothing
' save changes in the new document
ThisDrawing.Save
' do something with this document further
End Sub


~'J'~

lucas
12-21-2006, 11:04 AM
Another advantage of using the template is it also will have all of your text styles, lineweights, dim styles, etc.......ready to go.

Fatty is correct. You can copy the layers to a new file using vba but?! why? Especially if your creating an office standard. Using the template all files created from it will look the same.......

JHammond53
12-21-2006, 11:14 AM
Thanks for all of the replies...We currently have Template files set up with our layers, linetypes, and all of that sort. We want to do this as inserting because we feel it would be easier on us when we get CAD files from a surveyor we can easily just change their lines over to our layers.

We have now decided to instead of just having a VBA to insert the .dwg everytime we open an existing or now file, we are creating a tool bar that has a purge script to purge all unused layers, blocks and such just by clicking on the button, and we are working on creating a button to insert the drawing, this way it doesn't run every time we open a drawing and so we can just run it when we feel is necessary.

The reason why we went with a purge button is because we change layer properties every now and then, so when we are starting a new drawing we can just purge everything and reinsert everything by clicking on 2 buttons.

Sorry if this is hard to understand, it was hard to think of a way to word everything. Let me know if anything seems confusing and I will try to clear it up a little better.

lucas
12-21-2006, 11:20 AM
No...that makes perfect sense. Fatty has your solution, just save the .dvb and run it as he has shown. Just a note, I use wblock instead of purge....seems to clean things up nicely and quickly. Just select the entire drwg option when the dialog comes up.

lucas
12-21-2006, 11:24 AM
By the way welcome to vbaexpress. Same to you Fatty....I see your posts around in some of the cad forums...welcome to both of you

Tommy
12-21-2006, 11:50 AM
WOW I almost missed this one LOL

Hey Steve :hi:

Hi Fatty is that you on augi? (I think I remember the sig)

Hi JHammond53 :hi:

Welcome to VBAX!!

In the enviroment I work in we use acad.lsp to instert a dwg that has all of the linetypes, layers etc on start up and set a user1 variable in the acad doc that prevents the lsp from doing it more than once. The reason we do it this way is the drawings are generated from external programs as dxf files so some of the information is not set. (plotter define, paper layouts....) these all get set with this insert.

On a new drawing we have menu picks and lsp files for creating new drawings. The reason we do it that way is, it is easier for the user to get it done without picking the wrong template or un-updated template. Instead of having different templates for each condition (foundation, structural drawings, elevation & shop drawings )

Just a couple cents to throw in the disscusion. Let me know if I can be of help, looks like the problem has been well addressed and handled. :) :thumb

lucas
12-21-2006, 11:52 AM
Hi Tommy,
Good to see you. Merry Christmas

fixo
12-22-2006, 10:58 AM
Hi Tommy and lucas

Thanks for the kindly words

Yes, it's me on augi forum , but with another nickname :)

Merry Chrismas and Happy New Year

:hi: