Insert dwg in AutoCAD using VB-VBForums
Results 1 to 20 of 20

Thread: Insert dwg in AutoCAD using VB

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    17

    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

  2. #2
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,154

    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
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  3. #3
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,430

    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.

  4. #4
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    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?
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  5. #5
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,154

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  6. #6
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,430

    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.

  7. #7
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,154

    Re: Insert dwg in AutoCAD using VB

    Cool, Thanks Milk (got cookies? )

    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
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    17

    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
    Attached Images Attached Images  

  9. #9
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,154

    Re: Insert dwg in AutoCAD using VB

    Sounds like going with Milks suggestion on the commandline technique would work for you then (post #6).
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    17

    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

  11. #11
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,154

    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
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  12. #12
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,173

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

    I've been programming with VB for 17 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.


  13. #13
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,430

    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.
    Last edited by Milk; Jun 14th, 2008 at 08:25 AM.

  14. #14
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,430

    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:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. Dim Cmd As String
    5.    Cmd = "-insert" & vbCr & "F:\drawings\blocks\sanitary\S544354.dwg" & vbCr & vbCr & vbCr
    6.    Debug.Print SendCommands(Cmd)
    7. End Sub
    8.  
    9. Private Function SendCommands(CommandString As String) As Boolean
    10.    If Len(CommandString) - InStrRev(CommandString, vbCr) > 2 Then
    11.       CommandString = CommandString & vbCr
    12.    End If
    13.    With Label1
    14.       .LinkMode = 0 'Reset
    15.       '.LinkTopic = "AutoCAD.R15.DDE|system" 'Establish DDE link with Autocad 2000-2
    16.       .LinkTopic = "AutoCAD.R16.DDE|system" 'Autocad 2004-6 (unverified)
    17.       '.LinkTopic = "AutoCAD.R17.DDE|system" 'Autocad 2007-8 (unverified)
    18.       On Error Resume Next
    19.          .LinkMode = 2 'Manual connection
    20.          Select Case Err.Number
    21.             Case 0: 'Link Ok
    22.                .LinkExecute CommandString 'Send the console commands
    23.                SendCommands = True
    24.             Case 282: 'No Link ("No foreign application responded to a DDE initiate")
    25.                Debug.Print "Can't find Autocad DDE server" '
    26.             Case Else: 'Don't know
    27.                Debug.Print Err.Number, Err.Description
    28.          End Select
    29.       On Error GoTo 0
    30.    End With
    31. End Function
    to copy code quote the post

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    17

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

  16. #16
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,430

    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?

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    17

    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

  18. #18
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    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
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    17

    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

  20. #20
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,430

    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?

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.