.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

WblockCloneObjects and groups

1 REPLY 1
SOLVED
Reply
Message 1 of 2
_ane_
691 Views, 1 Reply

WblockCloneObjects and groups

 

Hello,

 

I want to copy a list of objects that are grouped in Autocad groups.

 

Actually I use WblockCloneObjects to copy the objects but the groups are not copied.

 

How can I copy the groups too?

 

thanks,

 

Ane

1 REPLY 1
Message 2 of 2
philippe.leefsma
in reply to: _ane_

Hi Ane,

 

You need to handle the group creation manually. Here is some code I wrote previously. What it does is prompting the user to select one entity, then it keeps track of all other entities in the same group. When you paste it in the destination database, it will copy all those entities and recreate the group:

 

class GroupInfo
{
    public string Name;
    public string Description;
    public ObjectIdCollection ObjectcIds;

    public GroupInfo(Group group)
    {
        Name = group.Name;
        Description = group.Description;

        ObjectcIds = new ObjectIdCollection();

        foreach (ObjectId id in group.GetAllEntityIds())
        {
            ObjectcIds.Add(id);
        }
    }
};

GroupInfo groupInfo = null;

[CommandMethod("CopyWithGroup")]
public void CopyWithGroup()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;

    //Entity selection
    PromptEntityOptions peo = new PromptEntityOptions(
        "\nSelect an entity to copy:");

    PromptEntityResult per = ed.GetEntity(peo);

    if (per.Status != PromptStatus.OK) 
        return;

    using (Transaction Tx = doc.TransactionManager.StartTransaction())
    {
        Entity entity = Tx.GetObject(
            per.ObjectId, 
            OpenMode.ForRead) as Entity;

        // Look references of selected entity 
        // to find if it belongs to a group
        ReferenceFiler refFiler = new ReferenceFiler();

        entity.DwgOut(refFiler);

        foreach (ObjectId sofPtId in refFiler.m_softPointerIds)
        {
            Object obj = Tx.GetObject(sofPtId, OpenMode.ForRead);

            // If we have a group, 
            // then add all its entities to the collection
            if (obj is Group)
            {
                groupInfo = new GroupInfo(obj as Group);
            }
        }
    }
}

[CommandMethod("PasteWithGroup")]
public void PasteWithGroup()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;

    if (groupInfo == null)
        return;
            
    using (Transaction Tx = doc.TransactionManager.StartTransaction())
    {
        BlockTable bt = Tx.GetObject(
            doc.Database.BlockTableId, 
            OpenMode.ForRead) 
                as BlockTable;

        BlockTableRecord btr = Tx.GetObject(
            bt[BlockTableRecord.ModelSpace], 
            OpenMode.ForWrite) 
                as BlockTableRecord;

        //Clone all entities in the destination database
        IdMapping idMap = new IdMapping();
        db.WblockCloneObjects(
            groupInfo.ObjectcIds, 
            btr.ObjectId, 
            idMap, 
            DuplicateRecordCloning.Ignore, 
            false);

        //Access group dictionary
        DBDictionary nod = Tx.GetObject(
            db.NamedObjectsDictionaryId, 
            OpenMode.ForRead) 
                as DBDictionary;

        DBDictionary groupDic = Tx.GetObject(
            nod.GetAt("ACAD_GROUP"), 
            OpenMode.ForRead) 
                as DBDictionary;

        // If the group doesn't exist then we create it 
        // and add to it the newly cloned entities
        if (!groupDic.Contains(groupInfo.Name))
        {
            groupDic.UpgradeOpen();

            Group group = new Group(
                groupInfo.Description, 
                true);

            groupDic.SetAt(groupInfo.Name, group);
            Tx.AddNewlyCreatedDBObject(group, true);

            System.Collections.IEnumerator enumerator = 
                idMap.GetEnumerator();

            while (enumerator.MoveNext())
            {
                IdPair idPair = (IdPair)enumerator.Current;

                // We need to check if we have an entity,
                // because some other objects are cloned
                // in the IdMapping during "WblockCloneObjects"
                if(idPair.Value.ObjectClass.IsDerivedFrom(
                    RXClass.GetClass(typeof(Entity))))
                    group.Append(idPair.Value);
            }
        }

        Tx.Commit();
    }
}


public class ReferenceFiler : DwgFiler
{
    public ObjectIdCollection m_hardPointerIds;
    public ObjectIdCollection m_softPointerIds;
    public ObjectIdCollection m_hardOwnershipIds;
    public ObjectIdCollection m_softOwnershipIds;

    public ReferenceFiler()
    {
        m_hardPointerIds = new ObjectIdCollection();
        m_softPointerIds = new ObjectIdCollection();
        m_hardOwnershipIds = new ObjectIdCollection();
        m_softOwnershipIds = new ObjectIdCollection();
    }

    public override ErrorStatus FilerStatus
    {
        get { return ErrorStatus.OK; }

        set { }
    }

    public override FilerType FilerType
    {
        get { return FilerType.IdFiler; }
    }

    public override long Position
    {
        get { return 0; }
    }

    public override IntPtr ReadAddress() { return new IntPtr(); }
    public override byte[] ReadBinaryChunk() { return null; }
    public override bool ReadBoolean() { return true; }
    public override byte ReadByte() { return new byte(); }
    public override void ReadBytes(byte[] value) { }
    public override double ReadDouble() { return 0.0; }
    public override Handle ReadHandle() { return new Handle(); }
    public override ObjectId ReadHardOwnershipId() { return ObjectId.Null; }
    public override ObjectId ReadHardPointerId() { return ObjectId.Null; }
    public override short ReadInt16() { return 0; }
    public override int ReadInt32() { return 0; }
    public override long ReadInt64() { return 0; }
    public override Point2d ReadPoint2d() { return new Point2d(); }
    public override Point3d ReadPoint3d() { return new Point3d(); }
    public override Scale3d ReadScale3d() { return new Scale3d(); }
    public override ObjectId ReadSoftOwnershipId() { return ObjectId.Null; }
    public override ObjectId ReadSoftPointerId() { return ObjectId.Null; }
    public override string ReadString() { return null; }
    public override ushort ReadUInt16() { return 0; }
    public override uint ReadUInt32() { return 0; }
    public override ulong ReadUInt64() { return 0; }
    public override Vector2d ReadVector2d() { return new Vector2d(); }
    public override Vector3d ReadVector3d() { return new Vector3d(); }

    public override void ResetFilerStatus() { }
    public override void Seek(long offset, int method) { }

    public override void WriteAddress(IntPtr value) { }
    public override void WriteBinaryChunk(byte[] chunk) { }
    public override void WriteBoolean(bool value) { }
    public override void WriteByte(byte value) { }
    public override void WriteBytes(byte[] value) { }
    public override void WriteDouble(double value) { }
    public override void WriteHandle(Handle handle) { }
    public override void WriteInt16(short value) { }
    public override void WriteInt32(int value) { }
    public override void WriteInt64(long value) { }
    public override void WritePoint2d(Point2d value) { }
    public override void WritePoint3d(Point3d value) { }
    public override void WriteScale3d(Scale3d value) { }
    public override void WriteString(string value) { }
    public override void WriteUInt16(ushort value) { }
    public override void WriteUInt32(uint value) { }
    public override void WriteUInt64(ulong value) { }
    public override void WriteVector2d(Vector2d value) { }
    public override void WriteVector3d(Vector3d value) { }

       
    public override void WriteHardOwnershipId(ObjectId value)
    {
        m_hardOwnershipIds.Add(value);
    }

    public override void WriteHardPointerId(ObjectId value)
    {
        m_hardPointerIds.Add(value);
    }

    public override void WriteSoftOwnershipId(ObjectId value)
    {
        m_softOwnershipIds.Add(value);
    }

    public override void WriteSoftPointerId(ObjectId value)
    {
        m_softPointerIds.Add(value);
    }

    public void reset()
    {
        m_hardPointerIds.Clear();
        m_softPointerIds.Clear();
        m_hardOwnershipIds.Clear();
        m_softOwnershipIds.Clear();
    }
}

I hope it helps,

Philippe.



Philippe Leefsma
Developer Technical Services
Autodesk Developer Network

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost