suwan116 Posted March 28, 2011 Share Posted March 28, 2011 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! Quote Link to comment Share on other sites More sharing options...
SLW210 Posted March 28, 2011 Share Posted March 28, 2011 I am moving your thread to the VBA section. Quote Link to comment Share on other sites More sharing options...
suwan116 Posted March 28, 2011 Author Share Posted March 28, 2011 Oh thanks for that! Hopefully i get the help i need Quote Link to comment Share on other sites More sharing options...
fixo Posted March 28, 2011 Share Posted March 28, 2011 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'~ Quote Link to comment Share on other sites More sharing options...
suwan116 Posted March 28, 2011 Author Share Posted March 28, 2011 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. Quote Link to comment Share on other sites More sharing options...
suwan116 Posted March 28, 2011 Author Share Posted March 28, 2011 I keep getting a debug error with this bit of code: Set myBlock = ThisDrawing.PaperSpace.InsertBlock(blockInsert, dwgName, 1, 1, 1, 0) Quote Link to comment Share on other sites More sharing options...
fixo Posted March 28, 2011 Share Posted March 28, 2011 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 Quote Link to comment Share on other sites More sharing options...
suwan116 Posted March 28, 2011 Author Share Posted March 28, 2011 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? Quote Link to comment Share on other sites More sharing options...
fixo Posted March 28, 2011 Share Posted March 28, 2011 (edited) 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 March 28, 2011 by fixo code added Quote Link to comment Share on other sites More sharing options...
suwan116 Posted March 28, 2011 Author Share Posted March 28, 2011 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. Quote Link to comment Share on other sites More sharing options...
fixo Posted March 29, 2011 Share Posted March 29, 2011 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 Quote Link to comment Share on other sites More sharing options...
suwan116 Posted March 29, 2011 Author Share Posted March 29, 2011 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. Quote Link to comment Share on other sites More sharing options...
fixo Posted March 29, 2011 Share Posted March 29, 2011 (edited) 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 March 29, 2011 by fixo spell check Quote Link to comment Share on other sites More sharing options...
suwan116 Posted March 29, 2011 Author Share Posted March 29, 2011 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. Quote Link to comment Share on other sites More sharing options...
haizang Posted April 15, 2011 Share Posted April 15, 2011 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? Quote Link to comment Share on other sites More sharing options...
suwan116 Posted April 15, 2011 Author Share Posted April 15, 2011 i dont have the work on this computer, but i used the coding on page 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.