How to Use filename in place of "ThisDrawing"

How to Use filename in place of "ThisDrawing"

Anonymous
Not applicable
3,889 Views
3 Replies
Message 1 of 4

How to Use filename in place of "ThisDrawing"

Anonymous
Not applicable

I am looking to replace the "thisdrawing" with the actual file name, any insight you can provide would be appreciated. I am looking to move this code out of the drawings so the "thisdrawing" won't be applicable

 

Public Sub BreakAllLines()

    Dim ent As AcadEntity
    Dim line As AcadLine
    Dim broken As Boolean
 
    Dim go As Boolean

    Do
        go = False
        For Each ent In ThisDrawing.ModelSpace
            If TypeOf ent Is AcadLine Then
                Set line = ent
                If BreakLine(line) Then
                    go = True
                    Exit For
                End If
            End If
        Next
    Loop Until Not go

End Sub
0 Likes
Accepted solutions (1)
3,890 Views
3 Replies
Replies (3)
Message 2 of 4

Ed__Jobe
Mentor
Mentor

No matter what module you put the code in, ThisDrawing will always be set to the active drawing.

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 4

Anonymous
Not applicable

Thanks Ed, 

 

but if I want to replace the "ThisDrawing." with the file name so I can run the code from Excel, how do I do that?

0 Likes
Message 4 of 4

norman.yuan
Mentor
Mentor
Accepted solution

ThisDrawing in AutoCAD VBA is a built-in reference to the ActiveDocument (AcadDocument) of the AcadApplication. You cannot replace it with a file name, which is a String.

 

When you automate AutoCAD from external application (Excel VBA, in your case), your code is responsible to obtain the target AcadDocument object to work with. Knowing a file name, you obvously need to open it in AutoCAD, upon opening, you get an AcadDocument object, and then work with it. Something like:

 

Dim fileName as String

fileName="C:\Temp\MyDrawing.Dwg"

 

Dim cadApp As AcadApplication

Dim dwg As AcadDocument

 

''Here goes the code to get an existing Acad application instance, or create a new one...

....

 

'' Then you open the drawing in AutoCAD

'' (or you can dertermine if the fiel has already open in existing Acad session, which I believe we have already discussed it previously)

Set dwg=cadApp.Documents.Open(fileName....)

 

'' Now you have the reference to the target AcadDocument, do whatever as you do with "ThisDrawing" in AutoCAD VBA: simply replace ThisDrawing with "dwg":

 

Dim ent As AcadEntity

For Each ent in dwg.ModelSpace

    ....

Next

Norman Yuan

Drive CAD With Code

EESignature