judge the entity where the original dwg file

judge the entity where the original dwg file

Anonymous
Not applicable
503 Views
1 Reply
Message 1 of 2

judge the entity where the original dwg file

Anonymous
Not applicable

i  copy  all entity  from  two dwgs  to  a new  dwg fille  ,  the  I  get all  the entity  in new file , how  to know  the entity 's  original  file ?

 public void Merge()
        {
            Point3d pt1;
            Point3d pt2;
            ObjectIdCollection SourceObjectIds1 = new ObjectIdCollection();
            ObjectIdCollection SourceObjectIds2 = new ObjectIdCollection();

            cp(@"E:\2017\P1.dwg", out pt1, SourceObjectIds1);
            cp(@"E:\2017\\P2.dwg", out pt2, SourceObjectIds2);

            ObjectIdCollection ToObjectid = new ObjectIdCollection();

            Database sourceDb = Application.DocumentManager.MdiActiveDocument.Database;
            using (Transaction extTrans = sourceDb.TransactionManager.StartTransaction())
            {
                BlockTable extBlockTable = (BlockTable)extTrans.GetObject(sourceDb.BlockTableId, OpenMode.ForRead);
                BlockTableRecord extModelSpace = (BlockTableRecord)extTrans.GetObject(extBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead);
                foreach (ObjectId id in extModelSpace)
                {
                    ToObjectid.Add(id);
                    // i want to know which original  dwg the  objectid from??????
                }

                extTrans.Commit();
            }
          

        }


public void cp(string drawingPath, out Point3d pt, ObjectIdCollection SourceObjectIds)
{
pt = new Point3d();

Database sourceDb = new Database(false, true);
sourceDb.ReadDwgFile(drawingPath, System.IO.FileShare.Read, false, null);


Autodesk.AutoCAD.DatabaseServices.TransactionManager SourceTm = sourceDb.TransactionManager;


using (Transaction extTrans = sourceDb.TransactionManager.StartTransaction())
{
BlockTable extBlockTable = (BlockTable)extTrans.GetObject(sourceDb.BlockTableId, OpenMode.ForRead);
BlockTableRecord extModelSpace = (BlockTableRecord)extTrans.GetObject(extBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead);
foreach (ObjectId id in extModelSpace)
{

SourceObjectIds.Add(id);
var ent = extTrans.GetObject(id, OpenMode.ForWrite, true);

if (ent is DBText)
{
DBText text = ent as DBText;
if (text.TextString == "aa")
{
pt = text.Position;
}
}


}


extTrans.Commit();
}
Database destDb = Application.DocumentManager.MdiActiveDocument.Database;

var mapping = new IdMapping();
var targetid = SymbolUtilityServices.GetBlockModelSpaceId(destDb);
var sourcedb = SourceObjectIds[0].Database;
sourcedb.WblockCloneObjects(SourceObjectIds, targetid, mapping, DuplicateRecordCloning.Ignore, false);


}
0 Likes
504 Views
1 Reply
Reply (1)
Message 2 of 2

ActivistInvestor
Mentor
Mentor

@Anonymous wrote:

i  copy  all entity  from  two dwgs  to  a new  dwg fille  ,  the  I  get all  the entity  in new file , how  to know  the entity 's  original  file ?


A cloned entity can't tell you what file it came from.

 

But, if you are cloning multiple files, you can record the value of the Database.HandSeed property before and after you clone the objects from each file.

 

Then, you can compare the handles of the cloned objects to the recorded HandSeed values, to distinguish what file the entity came from, presuming you don't create any new objects in the destination database after that. Use the Handle.Value property to convert the handle to a long, and if it is > than the recorded HandSeed before the object was cloned you know what clone operation produced the object.

 

Database destinationDb = ....

Handle beforeClone1 = destinationDb.HandSeed;

// <------ clone objects from another database here

Handle afterClone1 = destinationDb.HandSeed;

// <------ clone objects from another database here


// Here, if an entity's handle is > beforeClone1 and < afterClone1 then 
// it came from the first clone operation. If the entity's handle
// is > afterClone1, then it came from the second clone operation.

 

 

0 Likes