Author Topic: Inserting a block into a drawing  (Read 1428 times)

0 Members and 1 Guest are viewing this topic.

BAshworth

  • Guest
Inserting a block into a drawing
« on: June 18, 2007, 06:49:27 PM »
Ok, I'm evidently suffering from a huge brain fart, but I can't figure this out to save my life.

I'm taking a block from one drawing, and placing it into another.

I don't have a file I'm inserting, I'm not creating the block on the fly.

Something like ThisDrawing.Blocks.Add (MyBlockObject)

Any ideas?  Did that make sense?

Jeff_M

  • King Gator
  • Posts: 3942
  • C3D user & customizer
Re: Inserting a block into a drawing
« Reply #1 on: June 18, 2007, 07:02:42 PM »
CopyObjects ? Where the object to be copied is the Block definition in 1 drawing, and the destination is the Blocks collection of the second drawing?
« Last Edit: June 18, 2007, 07:07:36 PM by Jeff_M »

BAshworth

  • Guest
Re: Inserting a block into a drawing
« Reply #2 on: June 18, 2007, 07:23:08 PM »
CopyObjects ? Where the object to be copied is the Block definition in 1 drawing, and the destination is the Blocks collection of the second drawing?

Can't get that to work.

I tried the following

Code: [Select]
    Dim BlockCollection(0) As Object
    Dim NewDrawing As Variant
   
    NewDrawing = ThisDrawing.ModelSpace
    Set BlockCollection(0) = BlockObject
   
    ThisDrawing.CopyObjects BlockCollection, NewDrawing

With no success.  I get an "Object Not in Database" error.

You'd think this would be easier. lol

Jeff_M

  • King Gator
  • Posts: 3942
  • C3D user & customizer
Re: Inserting a block into a drawing
« Reply #3 on: June 18, 2007, 07:44:53 PM »
Not tested, but I think it should work.....*crosses fingers* (I really don't like posting untested code)I tested it and it works :-)
Code: [Select]

Function CopyBlock2ThisDrawing(OtherDrawing As AcadDocument, strBname As String) As Boolean
   
    Dim oBlock As AcadBlock
    On Error Resume Next
    Set oBlock = OtherDrawing.Blocks(strBname)
    If Err Then
        CopyBlock2ThisDrawing = False
        Exit Function
    End If
    Dim oCopyMe(0) As AcadObject
    Set oCopyMe(0) = oBlock
    OtherDrawing.CopyObjects oCopyMe, ThisDrawing.Blocks
    CopyBlock2ThisDrawing = True

End Function
« Last Edit: June 18, 2007, 07:50:23 PM by Jeff_M »

BAshworth

  • Guest
Re: Inserting a block into a drawing
« Reply #4 on: June 18, 2007, 08:04:34 PM »
Bah... nevermind.  I'm an idiot.  I had my source drawing as my new drawing in the copyobjects statement.

Thanks. That worked Like a charm.

Give me a sec to clean up some stuff and I'll post the full program.

Handy little tool, or it will be at least.
« Last Edit: June 18, 2007, 08:19:10 PM by BAshworth »

BAshworth

  • Guest
Re: Inserting a block into a drawing
« Reply #5 on: June 18, 2007, 08:16:47 PM »
Ok.. Here's the portion I was working on.  Thanks a heap Jeff.  This will get a block definition from an external file, without opening it with AutoCAD, and add it into the current drawing.

I'll leave the figuring out of which block to bring in, and the actual insertion routine to whatever your personal preference is.

Make sure to add a reference to AutoCAD/ObjectDBX Common (ver#) Type Library

Code: [Select]
Option Explicit

Function OpenSourceFile(FileName As String) As AXDBLib.AxDbDocument
   
    If Dir(FileName) <> "" Then
        Dim SourceDWG As New AXDBLib.AxDbDocument
        SourceDWG.Open (FileName)
       
        If Err.Number <> 0 Then
            If Err.Number <> -2147467259 Then 'File Moved
                SourceDWG.Open (FileName)
            End If
        End If
       
        Set OpenSourceFile = SourceDWG
    End If
           
End Function

Function ImportBlock(SourceName As String, BlockName As String) As AcadBlock

    Dim SourceDWG As New AXDBLib.AxDbDocument
    Dim EvryBlock As AcadBlock
   
    Set SourceDWG = OpenSourceFile(SourceName)
   
    For Each EvryBlock In SourceDWG.Blocks
        If UCase(BlockName) = UCase(EvryBlock.Name) Then
            Set ImportBlock = EvryBlock
        End If
    Next
   
    Dim BlockCollection(0) As AcadObject
   
    Set BlockCollection(0) = ImportBlock
    SourceDWG.CopyObjects BlockCollection, ThisDrawing.Blocks
   
   Set SourceDWG = Nothing
   
End Function

Sub Palette_Helper_Blocks()
   
    Dim SourceFile As String
    Dim BlockName As String
       
    SourceFile = "ContentMaster.dwg"
    BlockName = "< Name of Block To Insert >"
   
    ImportBlock SourceFile, BlockName
   
End Sub


Thanks also to jbuzzbee for pointing me down this direction.
« Last Edit: June 18, 2007, 08:18:45 PM by BAshworth »

Jeff_M

  • King Gator
  • Posts: 3942
  • C3D user & customizer
Re: Inserting a block into a drawing
« Reply #6 on: June 18, 2007, 08:22:21 PM »
I thought you might be trying this approach, 'tis why I opted to only show between 2 open drawings...I knew you'd figure out the rest. :-)