Creating new block with objects from object collection
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I need to create a new block, one per drawing from a list of drawings, and port them into a drawing at specific locations that will be identified by code (business logic for this will be given to me later but moving the block around based on a specific insertion point should not be difficult at all)
I have a series of drawings, each with a standard block that I will know the name of, that encircle (but do not contain) a number of non standard items. I have code that can open the drawing and find all objects contained within the boundary of the block, so I now have all the items that need to go into the new block and I should be able to translate coordinates based on the bottom center of my bounding block. However, these objects are a random assortment of other objects. Drawing squares, circles, text, even other blocks. What I want is a way to copy and paste these objects into the new block reference. 3 separate times now chatgpt has given me different answers on how to do this and the methods that it's suggesting simply do not exist at all. Literally the methods it suggests do not exist on the block reference. How do I add these items onto a new block reference I'm creating? I'm sure once I do that it should be as simple as copying the block definition, pasting it into the new drawing, and then moving the insertion point to the correct location.
Sub Main()
Dim objAcad As AcadApplication
Dim objDoc As AcadDocument
Dim objEnt As AcadEntity
Dim obj As AcadEntity
Dim objBlock As AcadBlockReference
Dim insideObjects As Collection
Set insideObjects = New Collection
Dim blockDef As AcadBlock
Dim blockRef As AcadBlockReference
Dim blkName As String
'Dim basePoint As Variant
Dim basePoint As Point3D
blkName = "Test"
basePoint(0) = 0
basePoint(1) = 0
basePoint(2) = 0
'basePoint = ThisDrawing.Utility.GetPoint(, "Specify base point for the block: ")
'Dim ssetObj As AcadSelectionSet
'For Each ssetObj In ThisDrawing.SelectionSets
'If ssetObj.Name = "TEST_SSET2" Then
'ssetObj.Delete
'Exit For
'End If
'Next ssetObj
Dim min(0 To 2) As Double
Dim max(0 To 2) As Double
Dim StartPoint As Variant
Dim EndPoint As Variant
Set objAcad = ThisDrawing.Application
Set objDoc = objAcad.ActiveDocument
For Each objEnt In objDoc.ModelSpace
If objEnt.ObjectName = "AcDbBlockReference" Then
Set objBlock = objEnt
Set blockDef = objDoc.Blocks.Add(basePoint, blkName)
If objBlock.Name = "Encasing Block name here" Then
Call objBlock.GetBoundingBox(min, max)
For Each obj In ThisDrawing.ModelSpace
Dim objMin(0 To 2) As Double
Dim objMax(0 To 2) As Double
On Error Resume Next
obj.GetBoundingBox objMin, objMax
If Err.Number = 0 Then
' Check if the object is within the block's bounding box
If IsWithinBoundingBox(objMin, objMax, min, max) Then
insideObjects.Add obj
'I can either add them to my collection or I can add them directly to the new block reference. Whatever is easiest.
End If
End If
On Error GoTo 0
Next obj
Set blockRef = objDoc.ModelSpace.InsertBlock(basePoint, blkName, 1, 1, 1, 0)
End If
End If
Next objEnt
End Sub