Jump to content

Open .dwg files from combobox with AutoCAD 2010 VBA


suwan116

Recommended Posts

Hey Guys!

 

Im new to AutoCAD and VBA. My task is to develop an autocad add on program that will allow a user to insert a customised project drawing frame. I have setup the userform where a user can click on the combo box and select a drawing frame (in this case A4, A4 Portrait, A3 and A1) and click the insert button, which will then display the frame in PaperSpace.

 

Currently i have used some code which enables me to bring up the drawing frame and display it in PaperSpace, however the code only allows me to bring up one of the frames. Im not sure how to change the code so the use can chose which frame they would like. Heres the code i have so far:

 

Private Sub CommandButton2_Click()

Dim myBlock As AcadBlockReference

Dim blockInsert(0 To 2) As Double

 

blockInsert(0) = -18

blockInsert(1) = -6

blockInsert(2) = 0

 

Set myBlock = ThisDrawing.PaperSpace.InsertBlock(blockInsert, "C:\Drawing Frame\A1_Frame.dwg", 1, 1, 1, 0)

 

End Sub

 

As you can see from the code, it will only bring up the A1 Frame. How could i edit this code so the end user could use the drop down to select a desired frame.

 

I hope this makes sence and hope you could help me with this.

 

Cheers!

Link to comment
Share on other sites

You might be want to use something like:

 

Say you have the items in your Combo:

A1

A3

A4

A4 portrait

etc...

Here is the code snip

 
Option Explicit
'' your folder path here:
Const frameFolder As String = "C:\Drawing Frame\" '<--backslash at the end
Private Sub CommandButton2_Click()
Dim myBlock As AcadBlockReference
Dim blockInsert(0 To 2) As Double
Dim frameDwg As String
frameDwg = ComboBox1.Text '<-- get selected item
Dim dwgName As String
dwgName = frameFolder & frameDwg & "_Frame.dwg"
MsgBox dwgName
blockInsert(0) = -18
blockInsert(1) = -6
blockInsert(2) = 0
Set myBlock = ThisDrawing.PaperSpace.InsertBlock(blockInsert, dwgName, 1, 1, 1, 0)
ComboBox1.ListIndex = -1 '<--refresh combo
End Sub

 

And also, no offence, but as far as you just started with programming, so

forget about VBA and start to learn AutoLisp or VB.NET (or both of them

if you like it, coz VBA would not supported in the future AutoCAD releases)

Just IMHO

 

~'J'~

Link to comment
Share on other sites

Hey, cheers for that, i shall give it a go!

 

And im doing this for an assignment. i dont think we can use vb.net or autolisp.

Link to comment
Share on other sites

I keep getting a debug error with this bit of code:

 

Set myBlock = ThisDrawing.PaperSpace.InsertBlock(blockInsert, dwgName, 1, 1, 1, 0)

Link to comment
Share on other sites

You have to check if you created correct drawing name

I've added FileExist function,

see comments

 
''Go to Tools->References, add Reference to Microsoft Scripting Runtime
''Go to Tools->Options->General->Error Trapping frame check  "Break on Unhandled Errors" button
Option Explicit
'' your folder path here:
Const frameFolder As String = "C:\Drawing Frame\" '<--backslash at the end

Function FileExist(dwgName As String) As Boolean
   Dim txt As String
   Dim fs As Object
   Dim fl As Object
   
   Set fs = CreateObject("Scripting.FileSystemObject")
    On Error GoTo Err_Control
   Set fl = fs.Getfile(dwgName)
   If Not fl Is Nothing Then
   FileExist = True
   Exit Function
   End If
Err_Control:
   FileExist = False
End Function
Private Sub CommandButton1_Click()
Dim frameDwg As String
frameDwg = ComboBox1.Text
Dim dwgName As String
dwgName = frameFolder & frameDwg & ".dwg" '<--build a full path of drawing here
MsgBox FileExist(dwgName)
End Sub

Private Sub CommandButton2_Click()
Dim myBlock As AcadBlockReference
Dim blockInsert(0 To 2) As Double
Dim frameDwg As String
frameDwg = ComboBox1.Text '<-- get selected item
Dim dwgName As String
dwgName = frameFolder & frameDwg & ".dwg" '<--build a full path of drawing here
If Not FileExist(dwgName) Then
MsgBox "File:" & vbCr & dwgName & " does not exists"
Exit Sub
End If
MsgBox dwgName
blockInsert(0) = -18
blockInsert(1) = -6
blockInsert(2) = 0
Set myBlock = ThisDrawing.PaperSpace.InsertBlock(blockInsert, dwgName, 1, 1, 1, 0)
ComboBox1.ListIndex = -1 '<--refresh combo
End Sub

Link to comment
Share on other sites

OMG! It worked!!! Wooooo! lol. Thanks very much for your help mate.

 

Just another thing, i can get the selected drawing frames to display in PaperSpace, but i have to manually navigate to the drawing by going to AutoCad then Layout 1.

 

Is there any code i could put at the end that takes the user to Paper Space, or do they do that manually?

Link to comment
Share on other sites

Just from the memory : take look at

ThisDrawing.ActiveLayout

in the Help

 

 
With ThisDrawing
.ActiveLayout = .Layouts.Item("Layout1")
End With

Or

 
With ThisDrawing
.ActiveLayout = .Layouts("Layout1")
End With

 

~'J'~

Edited by fixo
code added
Link to comment
Share on other sites

hey! i tried the code and It kinda works. Now i only need to click on autocad on the taskbar, and it will be on layout1. It doesnt automatically open autocad.

Link to comment
Share on other sites

You have to read docs about document-level event handling

in this case you need intercept the AcadDocument_Activate event

I'm aware of that sorry

Link to comment
Share on other sites

hey, i will do a search for this coding.

 

do you know a way to update the drawing, so if i launch the form agian it will insert new drawing and delete the previous frame.

Link to comment
Share on other sites

After his block was inserted you could be to store its handle, say in Textbox field, that is easiest

in the current program

 
Set myBlock = ThisDrawing.PaperSpace.InsertBlock(blockInsert, dwgName, 1, 1, 1, 0)
dim handStr as string
handStr=myBlock.Handle
TextBox1.Text=handStr

Then if you need to insert new one instead

you could be able to rich at old block using

HandleToObject method (see Help)

 

Don't remember exactly maybe something like

 
Dim oldObj as AcadObject ' or AcadEntity perhaps
Set oldObj=Thisdrawing.HandleToObject(Trim(TextBox1.Text))
oldObj.Delete
Set oldObj=Nothing

 

~'J'~

Edited by fixo
spell check
Link to comment
Share on other sites

I used this bit of code to auto show layout1

 

With ThisDrawing

.ActiveLayout = .Layouts.Item("Layout1")

.Application.Application.GetAcadState

End With

 

However im struggling with the update code.

Link to comment
Share on other sites

  • 3 weeks later...

I am new to this and have exactly the same application. Can you show your complete work here, starting with creating your combobox to inserting seletion to the drawing.

 

 

Thanks

 

OMG! It worked!!! Wooooo! lol. Thanks very much for your help mate.

 

Just another thing, i can get the selected drawing frames to display in PaperSpace, but i have to manually navigate to the drawing by going to AutoCad then Layout 1.

 

Is there any code i could put at the end that takes the user to Paper Space, or do they do that manually?

Link to comment
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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

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