PDA

View Full Version : Open AutoCAD drawing from vb6



shamsam1
03-04-2009, 11:56 PM
I have to opened dwg drawing from vb6 on click of a button. I am using the following code to open a dwg drawing and send command to commond prompt of autocad. But when I click on the command button once again, it is opening once again as new. But I want to activate the drawing which I have already opened and kept it minimized and pass once again command to command prompt.


Dim doc As AcadDocument
Dim acadapp As Object
Set acadapp = CreateObject("AutoCAD.Application")
Set doc = acadapp.Documents.Open("c:\123.dwg")
str = "mycommand" & vbLf
doc.SendCommand str

Tommy
03-05-2009, 07:25 AM
Why not use GetObject and if there is an error use the CreateObject method?

shamsam1
03-05-2009, 09:25 PM
If AutoCAD drawing is already opened then GetObject is used to make it activate. If not error part is executed.Can you suggest me any other code

Tommy
03-06-2009, 08:08 AM
This resolves the problem you are having. I have tested this on version 2007. This is also what I was trying to describe to you. If this is not what you are looking for, post all of the code that is giving you trouble and spell it out to me exactly what is the trouble and what you are trying to accomplish. This way I will not think you are trying to get me to do your job for you. I am here just to help, not do it for you.

Function GetAcad() As AcadApplication
Dim acadapp As Object
On Error Resume Next
Set acadapp = GetObject(, "AutoCAD.Application")
If Err.Number <> 0 Then
Set acadapp = CreateObject("AutoCAD.Application")
acadapp.Visible = True
Err.Clear
End If
Set GetAcad = acadapp
Set acadapp = Nothing
On Error GoTo 0
End Function
Function FinDwg(iAcadApp As AcadApplication, iDwg As String) As AcadDocument
'iDwg containts the full path
Dim mDoc As AcadDocument, mDocName As String
mDocName = Mid(iDwg, InStrRev(iDwg, "\") + 1)
For Each mDoc In iAcadApp.Documents
If mDoc.Name = mDocName Then
Set FinDwg = mDoc
Exit Function
End If
Next
Set FinDwg = iAcadApp.Documents.Open(iDwg)
End Function
Sub Doit()
Dim doc As AcadDocument
Dim acadapp As Object
Set acadapp = GetAcad
Set doc = FinDwg(acadapp, "c:\123.dwg")
Str = "mycommand" & vbLf
doc.SendCommand Str
End Sub



Happy Coding :beerchug:

shamsam1
03-06-2009, 10:53 PM
It works great.

I have a query I have opened a new AutoCAD drawing and I have not saved it and kept it minimized. Is it possible to pass command to that not saved drawing from vb6.

shamsam1
03-06-2009, 11:42 PM
thanks Tommy for guiding me.

It works great.

I have a query I have opened a new AutoCAD drawing and I have not saved it and kept it minimized. Is it possible to pass command to that not saved drawing from vb6.

Tommy
03-08-2009, 09:13 AM
Yes, this will draw a line in modelspace.

Sub Doit()
Dim doc As AcadDocument
Dim acadapp As Object, StrPts(2) As Double, EndPt(2) As Double
Set acadapp = GetAcad
Set doc = FinDwg(acadapp, "C:\Acad\1.dwg")
doc.WindowState = acMin
EndPt(0) = 1: EndPt(1) = 1: EndPt(2) = 1
StrPts(0) = 0: StrPts(1) = 0: StrPts(2) = 0
doc.ModelSpace.AddLine StrPts, EndPt
' Str = "mycommand" & vbLf
' doc.SendCommand Str
End Sub

suns_444
06-26-2013, 12:35 AM
hi tommy the above code you have give

will it open autocad application with specific drawing sheet

is it possible pls tell me how

regards
Sunny

Tommy
06-26-2013, 05:03 AM
Hi Sunny,

I am unsure as to what you are asking.
Acad has a new thing called sheet sets that I have no knowledge of other than I know it is there. :) But if you are asking about a specific drawing this is what I have already posted.
Let me know what you are looking for and please be specific so I can gain a better understanding of your requirements.

Regards
Tommy

suns_444
08-28-2013, 07:13 AM
what I was asking is that I need the code to open autocad application with specific inbuilt sheet acadiso.dwg

hence was asking that does above code that you posted does also help in opening as specific drawing sheet or not

if not I need code to open acad application with a particular drawing sheet

Tommy
09-04-2013, 10:03 AM
Post #4 has the code to open acad and a specific drawing.
Sub Doit()
Dim doc As AcadDocument
Dim acadapp As Object
Set acadapp = GetAcad
Set doc = FinDwg(acadapp, "c:\123.dwg") ' RIGHT HERE it will find acad and then open drawing 123
Str = "mycommand" & vbLf
doc.SendCommand Str
End Sub

suns_444
11-26-2013, 11:29 AM
thanx for the reply tommy that helped alot

i have new issue now i have created a vba code in vba editor in autocad workes fine there

but when i exported to vb6 it given me error variable not define for "thisdrwaing" code

cannot understand how define that variable

and also need know how to run vba code in autocad after launching in in vb6 via above code

thanx in advance

Regards
Sunny