Author Topic: Create Block Using SelectionSet  (Read 2059 times)

0 Members and 1 Guest are viewing this topic.

TJK44

  • Guest
Create Block Using SelectionSet
« on: October 26, 2011, 08:30:48 AM »
Hello all,

This is my first post here. Just got started programming AutoCAD about two days ago. In need of some help already. I am getting an error that is telling me eAlreadyInDB, unclear what this means or what to do to fix this. Here is the code I am using currently. The error is occurring  at this snippet of code.

Code: [Select]
newBlockDef.AppendEntity(acEnt)

Code: [Select]
        <CommandMethod("CreateBlock")> _
        Public Sub SelectEnts()

            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor

            Dim dwg As Database = doc.Database

            Dim res As PromptSelectionResult = ed.GetSelection
            Dim SS As Autodesk.AutoCAD.EditorInput.SelectionSet = res.Value
            Dim idArray As ObjectId() = SS.GetObjectIds()
            Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = dwg.TransactionManager

            Dim trans As Transaction = tm.StartTransaction
            If res.Status = PromptStatus.OK Then


                Try

                    ' create the new block definition             
                    Dim newBlockDef As BlockTableRecord = New BlockTableRecord
             
                    newBlockDef.Name = "CreateBlockTest"

               
                    Dim blockTable As BlockTable = trans.GetObject(dwg.BlockTableId, OpenMode.ForRead)

                 
                    If (blockTable.Has("CreateBlockTest") = False) Then


                        blockTable.UpgradeOpen()

                        ' Add the BlockTableRecord
                        blockTable.Add(newBlockDef)

                        trans.AddNewlyCreatedDBObject(newBlockDef, True)

                       
                        Dim acSSet As SelectionSet = res.Value
                        For Each acSSobj As SelectedObject In acSSet
                            If Not IsDBNull(acSSobj) Then
                                Dim acEnt As Entity = trans.GetObject(acSSobj.ObjectId, OpenMode.ForRead)
                                If Not IsDBNull(acEnt) Then
                                    newBlockDef.AppendEntity(acEnt)
                                End If
                            End If
                        Next

                   

                        Dim blockRefPointOptions As PromptPointOptions = New PromptPointOptions("Pick insertion point of BlockRef : ")

                     
                        Dim blockRefPointResult As PromptPointResult = ed.GetPoint(blockRefPointOptions)

                        ' test the Status of the PromptPointResult.
                        If (blockRefPointResult.Status <> PromptStatus.OK) Then

                            'If we got here then the GetPoint failed
                            trans.Dispose()
                           
                            Return
                        End If

                        'Declare a BlockReference variable
                        Dim blockRef As BlockReference = New BlockReference(blockRefPointResult.Value, newBlockDef.ObjectId)

                       
                        Dim curSpace As BlockTableRecord = trans.GetObject(dwg.CurrentSpaceId, OpenMode.ForWrite)

                       
                        curSpace.AppendEntity(blockRef)

                        ' Tell the transaction about the new block reference
                        trans.AddNewlyCreatedDBObject(blockRef, True)

                        ' Commit the transaction
                        trans.Commit()
                    End If
                Catch ex As Exception
                    ' If an error occurs the details of the problem will be printed
                    ' on the AutoCAD command line.
                    ed.WriteMessage("a problem occured because " + ex.Message)
                Finally
                    'Dispose the transaction by calling the Dispose method
             
                    trans.Dispose()

                End Try
            End If
        End Sub

Thanks in advance,
Ted

Krushert

  • Seagull
  • Posts: 13679
  • FREE BEER Tomorrow!!
Re: Create Block Using SelectionSet
« Reply #1 on: October 26, 2011, 09:36:04 AM »
Ted

First, I just want to say Welcome to The Swamp.
Second, I know nothing about net but there are a lot of folks here that do and they will be along shortly.
Third, Nice initials.   :-D

Ted
I + XI = X is true ...  ... if you change your perspective.

I no longer CAD or Model, I just hang out here picking up the empties beer cans

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Create Block Using SelectionSet
« Reply #2 on: October 26, 2011, 10:30:50 AM »
SS.AllowDuplicates = false;?

kaefer

  • Guest
Re: Create Block Using SelectionSet
« Reply #3 on: October 26, 2011, 10:45:39 AM »
Code: [Select]
newBlockDef.AppendEntity(acEnt)

Hi, be welcome, Ted!

Just what the error said: That Entity was already added to the Database. You have two options, just like the BLOCK command offers:

1) Keep the Entitys, that is, they have to be cloned.
Code: [Select]
                        For Each id In res.Value.GetObjectIds()
                            Dim ent = TryCast(tr.GetObject(id, OpenMode.ForRead), Entity)
                            If ent <> Nothing Then
                                Dim newent = TryCast(ent.Clone(), Entity)
                                If newent <> Nothing Then
                                    newBlockDef.AppendEntity(ent)
                                End If
                            End If
                        Next

2) Convert them into a block.
Code: [Select]
                        Dim oidc = New ObjectIdCollection()
                        For Each id In res.Value.GetObjectIds()
                            If id.ObjectClass.IsDerivedFrom(RXClass.GetClass(GetType(Entity))) Then
                                oidc.Add(id)
                            End If
                        Next
                        newBlockDef.AssumeOwnershipOf(oidc)

Cheers!

TJK44

  • Guest
Re: Create Block Using SelectionSet
« Reply #4 on: October 26, 2011, 10:52:20 AM »
Thank you to both kaefer and Bryco. I resolved the issue with kaefer's suggestion. So, let me get this straight, it was giving me that error because I was just appending the entity, so it was seeing the same entity twice, and it needs to be cloned so it will have a different ObjectID when added to the Block?