Working with making blocks from existing entities in a file. I've run into problems with iterating through the entities within a selection set and writing them to my new blocktablerecord. How do I take those items from my selection set and add them to this new block? All thoughts and help are appreciated.
Solved! Go to Solution.
Solved by Hallex. Go to Solution.
You can:
Thanks,
I like the "AssumeOwnershipOf" route that you suggest. When I use this on the selection set I'm told that the set cannot be converted into and ObjectIDCollection. May this I'm missing a simple step in working with selection sets. Is there an easy way to take the objects from the selection set, create a suitable IDCollection and then pass that collection to the "AssumeOwnershipOf" method?
Yup. The ObjectIdCollection constructor can take an ObjectId array as an argument, which is what SelectionSet.GetObjectIds() provides.
Put another way...
Dim ids as ObjectIdCollection = New ObjectIdCollection(sset.GetObjectIds)
Something like this
Just a hint starting the new thread you will be able to get a much more explanations
because of hiding your question at the end of thread
Imports System Imports System.Text Imports acApp = Autodesk.AutoCAD.ApplicationServices.Application Imports Autodesk.AutoCAD.Runtime Imports Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.EditorInput Imports Autodesk.AutoCAD.Geometry Public Class TesterCommands <CommandMethod("bobo", CommandFlags.UsePickSet)> _ Public Sub MakeBlockFromSelection() Dim doc As Document = acApp.DocumentManager.MdiActiveDocument Dim db As Database = doc.Database Dim ed As Editor = doc.Editor Using docloc As DocumentLock = doc.LockDocument Using tr As Transaction = db.TransactionManager.StartTransaction Try Dim pso As New PromptSelectionOptions() pso.MessageForRemoval = vbLf & " >> Nothing selected...." pso.MessageForAdding = vbLf & " >> Select objects >> " pso.AllowDuplicates = False pso.SingleOnly = True Dim res As PromptSelectionResult = ed.GetSelection(pso) If res.Status <> PromptStatus.OK Then Return End If Dim ids As ObjectId() = res.Value.GetObjectIds() If ids.Length = 0 Then Return End If Dim ppo As New PromptPointOptions(vbLf & "Pick insertion point for newly created block >>") Dim pres As PromptPointResult = ed.GetPoint(ppo) If pres.Status <> PromptStatus.OK Then Return End If Dim ip As Point3d = pres.Value Dim bt As BlockTable = DirectCast(db.BlockTableId.GetObject(OpenMode.ForWrite), BlockTable) Dim newname As String = "MyCoolBlock" If bt.Has(newname) Then ed.WriteMessage(vbLf & "Block ""{0}"" already exist", newname) Return End If Dim bids As New ObjectIdCollection() For Each eid As ObjectId In ids bids.Add(eid) Next acApp.SetSystemVariable("clayer", "0") acApp.SetSystemVariable("cecolor", "bylayer") Dim nbtr As New BlockTableRecord() bt.Add(nbtr) nbtr.Name = newname nbtr.Origin = ip nbtr.BlockScaling = BlockScaling.Uniform nbtr.Explodable = True tr.AddNewlyCreatedDBObject(nbtr, True) Dim map As New IdMapping() db.DeepCloneObjects(bids, nbtr.ObjectId, map, False) Dim coll As New ObjectIdCollection() For Each pair As IdPair In map If pair.IsPrimary Then Dim ent As Entity = DirectCast(tr.GetObject(pair.Value, OpenMode.ForWrite), Entity) If ent IsNot Nothing Then ent.Layer = "0" ent.ColorIndex = 0 If (TypeOf (ent) Is AttributeDefinition) Then Dim att As AttributeDefinition = DirectCast(ent, AttributeDefinition) att.LockPositionInBlock = True End If '<-- color byblock coll.Add(ent.ObjectId) End If End If Next nbtr.AssumeOwnershipOf(coll) tr.Commit() Catch ex As Autodesk.AutoCAD.Runtime.Exception ed.WriteMessage(vbLf + ex.Message + vbLf + ex.StackTrace) End Try End Using End Using End Sub End Class
~'J'~
Thanks for your post. I thought that your previous comment to the original string was close to the sollution i was after so i tried targetting my question.
thanks for you reply. It worked great. I'll study through it to see what I missed on my own.
regards,
matt
You're welcome
Cheers 🙂
Oleg
~'J'~
Can't find what you're looking for? Ask the community or share your knowledge.