Consulting

Results 1 to 12 of 12

Thread: Open AutoCAD drawing from vb6

  1. #1
    VBAX Contributor
    Joined
    May 2008
    Location
    bangalore
    Posts
    199
    Location

    Open AutoCAD drawing from vb6

    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

  2. #2
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Why not use GetObject and if there is an error use the CreateObject method?

  3. #3
    VBAX Contributor
    Joined
    May 2008
    Location
    bangalore
    Posts
    199
    Location
    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

  4. #4
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    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.
    [vba]
    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

    [/vba]

    Happy Coding

  5. #5
    VBAX Contributor
    Joined
    May 2008
    Location
    bangalore
    Posts
    199
    Location
    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.
    Last edited by shamsam1; 03-06-2009 at 11:49 PM.

  6. #6
    VBAX Contributor
    Joined
    May 2008
    Location
    bangalore
    Posts
    199
    Location
    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.
    Last edited by shamsam1; 03-07-2009 at 03:02 AM.

  7. #7
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Yes, this will draw a line in modelspace.
    [VBA]
    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
    [/VBA]

  8. #8
    VBAX Newbie
    Joined
    Apr 2013
    Posts
    3
    Location
    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

  9. #9
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    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

  10. #10
    VBAX Newbie
    Joined
    Apr 2013
    Posts
    3
    Location
    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

  11. #11
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    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

  12. #12
    VBAX Newbie
    Joined
    Apr 2013
    Posts
    3
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •