TheSwamp
Code Red => VB(A) => Topic started by: BAshworth 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?
-
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?
-
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
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
-
Not tested, but I think it should work.....
*crosses fingers* (I really don't like posting untested code)I tested it and it works :-)
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
-
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.
-
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
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.
-
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. :-)