• This forum is the machine-generated translation of www.cad3d.it/forum1 - the Italian design community. Several terms are not translated correctly.

open from excel through vba a specific dwg

  • Thread starter Thread starter blintz
  • Start date Start date

blintz

Guest
Hello everyone
There are several codes on the network for both programs.
after this premise... the question
I did a program in excel that perfectly dialogues with autocad
ie in a few words the data (blocks and text) are transferred to the drawing
as I can tell vba that I have to open a specific dwg, with open autocad, otherwise msgbox
 
Come posso dire a vba che mi deve aprire uno specifico dwg said:
Sorry
I wanted to be clearer and specify that excel (vba) has to open a specific file
I currently use this code:
Code:
sub insertblocks()

    '--------------------------------------------------------------------------------------------------------------------------
    'inserts blocks in autocad using data - insertion point, block name/full path, scale factors, rotation angle - from excel.
    'note that the block name or the block path must already exists, otherwise nothing will be inserted.
    'the code uses late binding, so no reference to external autocad (type) library is required.
    'it goes without saying that autocad must be installed at your computer before running this code.
    
    'written by:    christos samaras
    'date:          21/04/2014
    'e-mail:        xristos.samaras@gmail.com
    'site:          http://www.myengineeringworld.net
    '--------------------------------------------------------------------------------------------------------------------------
        
    'declaring the necessary variables.
    dim acadapp                 as object
    dim acaddoc                 as object
    dim acadblock               as object
    dim lastrow                 as long
    dim i                       as long
    dim insertionpoint(0 to 2)  as double
    dim blockname               as string
    dim blockscale              as scalefactor
    dim rotationangle           as double
    
    'activate the coordinates sheet and find the last row.
    with sheets("attributes")
        .activate
        lastrow = .cells(.rows.count, "i").end(xlup).row
    end with
        
    'check if there are coordinates for at least one circle.
    if lastrow < 5 then
        msgbox "there are no coordinates for the insertion point!", vbcritical, "insertion point error"
        exit sub
    end if
    
    'check if autocad application is open. if is not opened create a new instance and make it visible.
    on error resume next
    set acadapp = getobject(, "autocad.application")
    if acadapp is nothing then
        set acadapp = createobject("autocad.application")
        acadapp.visible = true
    end if
    
    'check (again) if there is an autocad object.
    if acadapp is nothing then
        msgbox "sorry, it was impossible to start autocad!", vbcritical, "autocad error"
        exit sub
    end if
    on error goto 0
    
    'if there is no active drawing create a new one.
    on error resume next
    set acaddoc = acadapp.activedocument
    if acaddoc is nothing then
        set acaddoc = acadapp.documents.add
    end if
    on error goto 0

    'check if the active space is paper space and change it to model space.
    if acaddoc.activespace = 0 then '0 = acpaperspace in early binding
        acaddoc.activespace = 1     '1 = acmodelspace in early binding
    end if
    
    on error resume next
    'loop through all the rows and add the corresponding blocks in autocad.
    with sheets("attributes")
        for i = 5 to lastrow
            'set the block name.
            blockname = .range("l" & i).value
            'if the block name is not empty, insert the block.
            if blockname <> vbnullstring then
                'set the insertion point.
                insertionpoint(0) = .range("i" & i).value
                insertionpoint(1) = .range("j" & i).value
                insertionpoint(2) = .range("k" & i).value
                'initialize the optional parameters.
                blockscale.x = 1
                blockscale.y = 1
                blockscale.z = 1
                rotationangle = 0
                'set the optional parameters (if there are values on the corresponding ranges).
                if .range("m" & i).value <> vbnullstring then blockscale.x = .range("m" & i).value
                if .range("n" & i).value <> vbnullstring then blockscale.y = .range("n" & i).value
                if .range("o" & i).value <> vbnullstring then blockscale.z = .range("o" & i).value
                if .range("p" & i).value <> vbnullstring then rotationangle = .range("p" & i).value
                'add the block using the sheet data (insertion point, block name, scale factors and rotation angle).
                'the 0.0174532925 is to convert degrees into radians.
                set acadblock = acaddoc.modelspace.insertblock(insertionpoint, blockname, _
                                blockscale.x, blockscale.y, blockscale.z, rotationangle * 0.0174532925)
            end if
        next i
    end with
    'call extract
   ' call updateattr
    'zoom in to the drawing area.
    acadapp.zoomextents

    'release the objects.
    set acadblock = nothing
    set acaddoc = nothing
    set acadapp = nothing
    
    'inform the user about the process.
    msgbox "the blocks were successfully inserted in autocad!", vbinformation, "finished"

end sub
hope in a vs help hello
 

Forum statistics

Threads
44,997
Messages
339,767
Members
4
Latest member
ibt

Members online

No members online now.
Back
Top