Insert dwg in AutoCAD using VB
Hallo Friends,
I am new here today...searching a code in internet for following problem.
I have a drawing in AutoCAD2005.
I have written a VisualBasic program finding an Appropriate dwg files in Particular folders coz you people know how our Explorer works..in tree form...clicking clicking and so on till we need our particular folder..so inorder to minimize the time program works using command buttons and lists all the dwgs in Listview.
now i choose a dwg file in Listview ( i mean its a small drawing which should be inserted in opened drawing ). and click the Button INSERT.
now task is this dwg file which is been selected in Listview shold be inserted in opened autocad drawing.
Please can anybody help me how to write this code for this Button Insert..
Private sub insert_click()
.
.
.
.
.
End Sub
it would be very helpfull for me my friends..
i think i have expressed my task in detail..
Thanks - Singoi
Re: Insert dwg in AutoCAD using VB
You should be able to ShellExecute the dwg file so it opens with its associated program which is Autocad.
Code:
Option Explicit
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Private Const SW_HIDE As Long = 0
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWMINIMIZED As Long = 2
Private Sub Command1_Click()
ShellExecute Me.hwnd, "open", "C:\Users\Public\Test1.dwg", vbNullString, "C:\", SW_SHOWNORMAL
End Sub
Re: Insert dwg in AutoCAD using VB
Not sure but I think singoi means insert into an existing open drawing, not open separately.
Rather like the AutoCad command line Insert but with a treeview to choose the file.
Re: Insert dwg in AutoCAD using VB
If I remember correctly from past posts, AutoCad does have a form of VBA, so this sounds more like a job for that than for Visual Basic 6.
singoi: Can you record macros in AutoCad VBA?
Re: Insert dwg in AutoCAD using VB
Yes AutoCad does support VBA but that wont help in this situation. He has written his app to populate his listview with the dwg files. He wants to select one and open it either in a new AutoCad instance of in an existing instance.
If its new instance then my posted code will do it. If its an existing instance then will AutoCad load the dwg in the existing instance as a second window or will it create a secondary instance altogether?
Then there is the question if he wants the dwg to be added to the canvas in an existing drawing. This will be more difficult as most of us dont have autocad so unsure how that would be done.
Re: Insert dwg in AutoCAD using VB
I've got an older copy Acad2000, I was waiting for Singoi to post again to confirm what he/she wants. There is a built in dialog that can be invoked from AutoCads command line, it Inserts one drawing into another but it does not use a treeview. I imagine it would be pretty straight forward to make a friendlier version using the VBA component.
If it is the case that the drawing should be inserted into another drawing then the insertion point, and scale parameters might also be important.
Re: Insert dwg in AutoCAD using VB
Cool, Thanks Milk (got cookies? :D)
But it sounds like this is desired and being done already in VB 6 so it should be just starting auocad and passing those arguments to do an actual insert as you mentioned. Either way I think we can get it solved once we hear back
1 Attachment(s)
Re: Insert dwg in AutoCAD using VB
hallo My friends,
seems to be interesting when i go through my forum..haha..
well..my problem is not yet solved..i am attaching a small JPEG how my window looks like.there is a Listview which shows my dwgs in particular folder.
now i already opened a drawing (for example allias.dwg) and on my second monitor i open my program and i select going throough all my command buttons to my destination folder..and now i select my dwg here in listview.
i click insert.
now my task is this selected dwg should be inserted either as block or as normal dwg in Allias.dwg
now i dont need exact point where its supposed to be inserted..but should get inserted..later i can move this to the position where i need depending on the free space available.
I hope my friends have understood my task..
hoping a response from you.
thanks - Singoi
Re: Insert dwg in AutoCAD using VB
Sounds like going with Milks suggestion on the commandline technique would work for you then (post #6).
Re: Insert dwg in AutoCAD using VB
Hi friends,
I have been doing this project with different other features since weeks...and wanna include this feature also in my project..so i have continued doing..
as we know that our servers are full of different harddisks with different names like C,D,F bla bla bla...similarly we got loads of disks and loads of folders where users are going on searching for required dwg hours and hours..
this made me think to make this feature so that it will be easy and less time consuming in making tasks faster.
please help me in making this code for this button INSERT...
please please pleaseeeeeeeee....searching since days for this code..
Regards,
Singoi
Re: Insert dwg in AutoCAD using VB
I dont have AutoCad so I would say you will have to either google for now or wait until Milk is back online as he has an older copy of AutoCad to work with;)
Re: Insert dwg in AutoCAD using VB
Quote:
Originally Posted by Hack
If I remember correctly from past posts, AutoCad does have a form of VBA, so this sounds more like a job for that than for Visual Basic 6.
I know when I install AutoCAD 2000 5 or 6 years years ago some of the AutoCAD applications were made with VB6 because it overite a lot of the VB ocx's with older versions, I wasn't to pleased. ;)
Re: Insert dwg in AutoCAD using VB
To my mind it would make a lot more sense to write an AutoCad helper program from within AutoCad it self, giving you full access to the Blocks and Drawings collections.
That said the command line window (Txt:"Marin" Cls:"Afx:400000:8:10011:10:0" for ACAD2000) is always open and accepts the WM_PASTE messages.
Copying this to the clipboard and sending the WM_PASTE message to the command window should insert <<The Blocks Path>> at 0,0,0. Scaled 1:1 in X and Y and rotated to 0 degrees. All relative to the WCS not the current UCS.
Code:
-insert
<<The Blocks Path>>
0,0,0
1
1
0
I guess your code will need to find all the AutoCad windows which are open, if more than one it will need to know which one. Check to see if a drawing is open. If a drawing is open then look for the Command Line child window. Copy the commands to the clipboard. Send the WM_PASTE message to the Command Line Window. Job Done.
If I remember correctly the command console can be set up on a different monitor.
It might be possible to link up between your App and Autocad on a lower level but I don't know how.
Edit: Have a look at Fazi's Sendmessage blaster, it's useful for working out whats going on window wise.
Re: Insert dwg in AutoCAD using VB
Ooh, it seems you can use DDE to do the same as above. (Send command strings to the console)
vb Code:
Option Explicit
Private Sub Command1_Click()
Dim Cmd As String
Cmd = "-insert" & vbCr & "F:\drawings\blocks\sanitary\S544354.dwg" & vbCr & vbCr & vbCr
Debug.Print SendCommands(Cmd)
End Sub
Private Function SendCommands(CommandString As String) As Boolean
If Len(CommandString) - InStrRev(CommandString, vbCr) > 2 Then
CommandString = CommandString & vbCr
End If
With Label1
.LinkMode = 0 'Reset
'.LinkTopic = "AutoCAD.R15.DDE|system" 'Establish DDE link with Autocad 2000-2
.LinkTopic = "AutoCAD.R16.DDE|system" 'Autocad 2004-6 (unverified)
'.LinkTopic = "AutoCAD.R17.DDE|system" 'Autocad 2007-8 (unverified)
On Error Resume Next
.LinkMode = 2 'Manual connection
Select Case Err.Number
Case 0: 'Link Ok
.LinkExecute CommandString 'Send the console commands
SendCommands = True
Case 282: 'No Link ("No foreign application responded to a DDE initiate")
Debug.Print "Can't find Autocad DDE server" '
Case Else: 'Don't know
Debug.Print Err.Number, Err.Description
End Select
On Error GoTo 0
End With
End Function
to copy code quote the post
1 Attachment(s)
Re: Insert dwg in AutoCAD using VB
Hi,
i have tried with this code..
Private Sub cmdins_Click()
Dim acadApp As acadapplication
Dim acDWG As AcadDocument
Dim block As String
Dim inspoint As Object
'Dim Scale As Double
block = ListView1.SelectedItem
Try
acadApp = trycast(marshal.getactiveobject("autocad.application"), acadapplication)
if typeof (acadApp) is acadapplication Then
If acadApp.Documents.Count > 0 Then
acDWG = acadApp.ActiveDocument
inspoint = acDWG.Utility.GetPoint(, "select Insertion point:")
acDWG.ModelSpace.InsertBlock(inspoint, block, scale, scale, scale, 0.0#)
Else
Msgbox("AutoCAD doesn't have any Drawings open.", MsgBoxStyle.Exclamation)
End If
catch ex As exception
EndTry
End Sub
but getting errors..not functioning.
I need the file which user selects in Listview inserted in opened drawing. Attaching a JPEG File...User has selected a file in Viewlist box..now presses Insert..this following File should get inserted in Opened Drawing...
Pleaseeeeeeeeeeeeeee friends..loosing hope coz not able to find the right code which can work...
Re: Insert dwg in AutoCAD using VB
The code you post looks like .net but the form you post looks like VB6, what language are you using?
Have you tried the DDE version I posted? If you give it a valid block path, it can insert it at the specified coordinate at the specified scale and rotation it the current active drawing. Is that not what you want?
Re: Insert dwg in AutoCAD using VB
Private Sub cmdins_Click()
Dim acadApp As AcadApplication
Dim acDWG As AcadDocument
Dim block As String
Dim inspoint() As Object
Dim Scale1 As Double
block = ListView1.SelectedItem
Scale1 = 1#
'On Error GoTo errhandler:
On Error GoTo NoAutocad
Set acadApp = GetObject(, "AutoCAD.Application")
On Error GoTo 0
If TypeOf acadApp Is AcadApplication Then
If acadApp.Documents.Count > 0 Then
Set acDWG = acadApp.ActiveDocument
inspoint = acDWG.Utility.GetPoint(, "Select Insertion point:")
Dim blk As AcadBlockReference
Set blk = acDWG.ModelSpace.InsertBlock(inspoint, block, Scale1, Scale1, Scale1, 0#)
Else
MsgBox ("AutoCAD doesn't have any Drawings open.")
End If
Else
End If
Exit Sub
NoAutocad:
MsgBox "Autocad Not Started!", vbCritical
End Sub
i have changed the code...as above..
i am using Excel VBA Editor...starting will be in Excel and then VBA...using VB6...coz i dont have VB6 installed in my computer and my superiors dont wanna invest money on it..but want the program to be success..
cant be against them..and i want to make this programm run..
still i have problem in follwoing syntax
inspoint = acDWG.Utility.GetPoint(, "Select Insertion point:")
please can u help me...
VBA Language is German which i am using but code will be english..
Singoi
Re: Insert dwg in AutoCAD using VB
Quote:
Originally Posted by singoi
i am using Excel VBA Editor...starting will be in Excel and then VBA...using VB6...coz i dont have VB6 installed in my computer and my superiors dont wanna invest money on it..but want the program to be success..
Although it might be a little late in the game, if you need VBA code, then the question needs to be in the Office Development section.
Moved
Re: Insert dwg in AutoCAD using VB
hallo Hack,
Thank you..i would be happy if someone help me in some section..but i thank you for pushing me in right corner..
Regards - singoi
Re: Insert dwg in AutoCAD using VB
Quote:
Originally Posted by singoi
<snip>I am using Excel VBA Editor...
Dare I ask why your not using the AutoCAD VBA Editor?