Accessing secondary .dwg files in the background

Accessing secondary .dwg files in the background

king
Enthusiast Enthusiast
736 Views
6 Replies
Message 1 of 7

Accessing secondary .dwg files in the background

king
Enthusiast
Enthusiast

I have a program that creates a sheet set based on several drawings layouts. Currently the code has to open all of the DWG files to grab the layouts contained in the files. How can I have this happen in the background so the user doesn't have to wait around for the program to open the files? I've seen some mentions of using accoreconsole.exe but can't really find much documentation or examples of how to implement it.

 

Thanks for any info 

0 Likes
Accepted solutions (3)
737 Views
6 Replies
Replies (6)
Message 2 of 7

Ed__Jobe
Mentor
Mentor
Accepted solution

Since you probably only need to manage 10-30 documents for a sheetset, I would recommend using VBA to open documents in the background, i.e. without using the editor. That's the part that takes a long time. Search this forum for "AxDbDocument" for some sample code.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 7

grobnik
Collaborator
Collaborator
Accepted solution

Hi @king here below an example of code using Ed suggestions

'Find & Replace text in multiples dwgs with VBA - Autodesk Community - AutoCAD

Sub TestDb()
Dim MyaDBX As AxDbDocument
Set MyaDBX = AcadApplication.GetInterfaceObject("ObjectDBX.AxDbDocument." & Left$(ThisDrawing.GetVariable("ACADVER"), 2))

MyPath = "C:\Users\....." ' Place here your path for opening the dwg

MyFileName = "testDb.dwg"
Call MyaDBX.Open(MyPath & MyFileName)
Dim MTextObj As AcadMText
    Dim corner(0 To 2) As Double
    Dim width As Double
    Dim text As String
    corner(0) = 0#: corner(1) = 10#: corner(2) = 0#
    width = 10
    text = "This is the text String for the mtext Object"

    ' Creates the mtext Object
    Set MTextObj = MyaDBX.ModelSpace.AddMText(corner, width, text)
    ZoomAll
     For Each objEntity In MyaDBX.ModelSpace
     
        If TypeOf objEntity Is AcadText Or TypeOf objEntity Is AcadMText Then
            Set MyobjEntity = objEntity
            Debug.Print MyobjEntity.TextString
        End If
        
    Next
    MyNewFileName = MyFileName & "001.dwg"

   MyaDBX.SaveAs (MyPath & MyNewFileName)
End Sub

 

Message 4 of 7

king
Enthusiast
Enthusiast
Accepted solution

Thank you for pointing me in the right direction. This is what I was able to come up with. Any feedback is appreciated

 

 

 

 

Sub ListLayouts()
    Dim acadApp As AcadApplication
    Dim acadDoc As AcadDocument
    Dim MyaDBX As Object
    Dim layout As AcadLayout
    Dim filePath As String
    Dim acadVer As String
    
    On Error GoTo ErrorHandler
    
    Set acadApp = GetObject(, "AutoCAD.Application")
    Set acadDoc = acadApp.ActiveDocument
    acadVer = Left$(acadDoc.GetVariable("ACADVER"), 2)
    Set MyaDBX = acadApp.GetInterfaceObject("ObjectDBX.AxDbDocument." & acadVer)
    filePath = "PLACEHOLDER"
    MyaDBX.Open filePath
    
    For Each layout In MyaDBX.layouts
    Next layout
    
    Set layout = Nothing
    Set MyaDBX = Nothing
    Set acadDoc = Nothing
    Set acadApp = Nothing
    
    Exit Sub

ErrorHandler:
    On Error Resume Next
    Set layout = Nothing
    Set MyaDBX = Nothing
    Set acadDoc = Nothing
    Set acadApp = Nothing
End Sub

 

 

 

 

0 Likes
Message 5 of 7

grobnik
Collaborator
Collaborator

Hi @king,

seems could be work, I guess that inside the for next cycle

    For Each layout In MyaDBX.layouts
    Next layout

You will execute what do you need to do in each layout.

I would like to highlight a couple of suggestions more:

1) Of course the drawing to be manipulate shall be not opened during the procedure execution (in case you can open an empty drawing in editor, and at the end of procedure open the manipulated drawing with editor to evaluate the results).

2) Save the dwg database after modifications at the end of procedure, I didn't see any document save inside the procedure (look at my code)

MyNewFileName = MyFileName & "001.dwg"

   MyaDBX.SaveAs (MyPath & MyNewFileName)

3) Could be necessary insert the reference library  Open inside VBA development section Tools->Reference and find 

Autocad / ObjectDBX..... library, release could be change it's depend of the Acad release do you have installed.

this is also the reason because we insert the ACADVER system variable.

 

grobnik_0-1725607528501.png

    acadVer = Left$(acadDoc.GetVariable("ACADVER"), 2)
    Set MyaDBX = acadApp.GetInterfaceObject("ObjectDBX.AxDbDocument." & acadVer)

Bye

 

 

Message 6 of 7

king
Enthusiast
Enthusiast

Thanks for the response. My code was mainly a test to see the methods for obtaining data from an unopened drawing.

 

I have a full program that is much larger that creates sheet sets and imports layouts, renames them, etc that I am going to implement this method into. 

 

Thanks for your help!

0 Likes
Message 7 of 7

grobnik
Collaborator
Collaborator
You asked for a feedback, so I made some further comments.
Hope could be helpful