wblockclone dbtext into side database and then turn it into a blocktablerecord

wblockclone dbtext into side database and then turn it into a blocktablerecord

Anonymous
Not applicable
1,196 Views
7 Replies
Message 1 of 8

wblockclone dbtext into side database and then turn it into a blocktablerecord

Anonymous
Not applicable

I'm trying to wblockclone a DBText object into a side database (into model space).  Once the DBText object is in the side database I want to create a new blocktablerecord and add that DBText to the blocktablerecord - essentially creating a new block composed of just the text object in the side database.  I've tried a few different approaches and no matter what I do I get to a point with I get a ePermanetlyErase error.  Any help would be greatly appreciated - I've wasted too many hours on just this tid bit of code within my project.  Thank you in advance!

 

(sorry I don't have any code to paste in here - since out of frustration I've erased what ever I had lol)

0 Likes
Accepted solutions (2)
1,197 Views
7 Replies
Replies (7)
Message 2 of 8

_gile
Consultant
Consultant

Hi,

 

have you simply try to first add the BlockTableRecord to the BlockTable of the side Database and then directly WblockClone the DBText in this BlockTableRecord?



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 8

_gile
Consultant
Consultant
Accepted solution

Here's an example:

 

        [CommandMethod("TEST")]
        public void Test()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var sourceDb = doc.Database;
            var ed = doc.Editor;
            var opts = new PromptEntityOptions("\nSelect text: ");
            opts.SetRejectMessage("\nSelected object is not a text.");
            opts.AddAllowedClass(typeof(DBText), true);
            var result = ed.GetEntity(opts);
            if (result.Status != PromptStatus.OK) return;
            var ids = new ObjectIdCollection();
            ids.Add(result.ObjectId);
            using (var targetDb = new Database(true, true))
            {
                ObjectId btrId;
                using (var tr = targetDb.TransactionManager.StartTransaction())
                {
                    var bt = (BlockTable)tr.GetObject(targetDb.BlockTableId, OpenMode.ForWrite);
                    var btr = new BlockTableRecord();
                    btr.Name = "test";
                    btrId = bt.Add(btr);
                    tr.AddNewlyCreatedDBObject(btr, true);
                    tr.Commit();
                }
                var mapping = new IdMapping();
                sourceDb.WblockCloneObjects(ids, btrId, mapping, DuplicateRecordCloning.Ignore, false);
                targetDb.SaveAs(@"C:\Temp\Foo.dwg", DwgVersion.Current);
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 8

Anonymous
Not applicable

thanks for the reply - I'll give that a shot and let you know

0 Likes
Message 5 of 8

Anonymous
Not applicable

still have the same issue...

 

here is my code  (note the source database is also a side database)  I get the error on the wblockcloneobjects line within section that handles "Text" objects.

 

 

Dim acSelectedDwgFileDatabase As Autodesk.AutoCAD.DatabaseServices.Database = New Autodesk.AutoCAD.DatabaseServices.Database
            acSelectedDwgFileDatabase.ReadDwgFile(SelectedDwgFile, Autodesk.AutoCAD.DatabaseServices.FileOpenMode.OpenForReadAndAllShare, False, vbNull.ToString)
            Using acSelectedDwgFileTransaction As Autodesk.AutoCAD.DatabaseServices.Transaction = acSelectedDwgFileDatabase.TransactionManager.StartTransaction
                'Dim acObjectId As Autodesk.AutoCAD.DatabaseServices.ObjectId = acSelectedDwgFileTransaction.GetObject(SelectedacObjectId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead).ObjectId
                Dim acObjectIdCollect As Autodesk.AutoCAD.DatabaseServices.ObjectIdCollection = New Autodesk.AutoCAD.DatabaseServices.ObjectIdCollection
                If SelectedObjecType = "Block" Then
                    Dim acObjectId As Autodesk.AutoCAD.DatabaseServices.ObjectId = acSelectedDwgFileDatabase.GetObjectId(False, New Autodesk.AutoCAD.DatabaseServices.Handle(SelectedacHandle), 0)
                    Dim acBlockReference As Autodesk.AutoCAD.DatabaseServices.BlockReference = acSelectedDwgFileTransaction.GetObject(acObjectId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead) 'TryCast(acSelectedDwgFileTransaction.GetObject(acObjectId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead), Autodesk.AutoCAD.DatabaseServices.BlockReference)
                    If acBlockReference IsNot Nothing Then
                        Dim acBlockTableRecord As Autodesk.AutoCAD.DatabaseServices.BlockTableRecord = acSelectedDwgFileTransaction.GetObject(acBlockReference.BlockTableRecord, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)
                        SampleUltraPictureBox.Image = ImageSourceToGDI(Autodesk.AutoCAD.Windows.Data.CMLContentSearchPreviews.GetBlockTRThumbnail(acBlockTableRecord))
                    End If
                End If
                If SelectedObjecType = "Text" Then
                    Dim acObjectId As Autodesk.AutoCAD.DatabaseServices.ObjectId = acSelectedDwgFileDatabase.GetObjectId(False, New Autodesk.AutoCAD.DatabaseServices.Handle(SelectedacHandle), 0)
                    acObjectIdCollect.Add(acObjectId)
                    Using targetDb = New Autodesk.AutoCAD.DatabaseServices.Database(True, True)
                        Dim btrid As Autodesk.AutoCAD.DatabaseServices.ObjectId
                        Dim btr = New Autodesk.AutoCAD.DatabaseServices.BlockTableRecord
                        Using tr = targetDb.TransactionManager.StartTransaction
                            Dim bt = CType(tr.GetObject(targetDb.BlockTableId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite), Autodesk.AutoCAD.DatabaseServices.BlockTable)
                            btr.Name = "sample"
                            btrid = bt.Add(btr)
                            tr.AddNewlyCreatedDBObject(btr, True)
                            tr.Commit()
                        End Using
                        Dim acmapping = New Autodesk.AutoCAD.DatabaseServices.IdMapping
                        acSelectedDwgFileDatabase.WblockCloneObjects(acObjectIdCollect, btrid, acmapping, Autodesk.AutoCAD.DatabaseServices.DuplicateRecordCloning.Ignore, False)
                        SampleUltraPictureBox.Image = ImageSourceToGDI(Autodesk.AutoCAD.Windows.Data.CMLContentSearchPreviews.GetBlockTRThumbnail(btr))
                    End Using
                End If
                acSelectedDwgFileDatabase.CloseInput(True)
            End Using
0 Likes
Message 6 of 8

_gile
Consultant
Consultant
Accepted solution

Sorry I have a hard time reading your code. In addition to being written in VB, it contains too many instructions that are not directly related to the subject and data that we do not know where they come from.

Just try to use the Database Constructor overload which takes two arguments:

Dim acSelectedDwgFileDatabase As Database = New Database (False, True)

You should also save the newly created database:

targetDb.SaveAs (...)

 

Could please tell which line of your code raise a ePermanetlyErase exception.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 8

william.klawitter4MJ74
Contributor
Contributor

Using Dim acSelectedDwgFileDatabase As Database = New Database (False, True) in my code solve the issue.  I bet all along the previous ways I was trying to wblock into the side database were fine and it was always simply because of this line.  However I'm going to keep your suggested code of writing directly into a newly create btr - it seems cleaner than what I had before.

 

(btw sorry the code is messy - it's just prototype code at this point as I'm testing some ideas for our project)

 

Thank you for the help!!

0 Likes
Message 8 of 8


@william.klawitter4MJ74 wrote:

 

(btw sorry the code is messy - it's just prototype code at this point as I'm testing some ideas for our project)

 


If you want to encourage others to read your code, try to avoid using fully-qualified identifiers with namespace prefixes.

 

Unfortunately, many people will not bother looking at code like that, because of how difficult is is to read.