.NET

Reply
Mentor
bsee1
Posts: 208
Registered: ‎11-14-2011
Message 1 of 5 (188 Views)

Swap titleblock between 2 drawings

188 Views, 4 Replies
02-04-2013 11:38 AM

I have 2 files and I'd like to copy a titleblock from a template drawing and use it to replace an existing titleblock on an in progress drawing.  The titleblock on the in progress drawing has attributes, and I'd like to keep them when I put in the new titleblock.  The attribute tag names between the 2 titleblocks are the same.

I do not know the exact names of the titleblocks, only that they contain the words "title block" somewhere in their name.

 

I've developed for Inventor for a while now, but I'm completely new to AutoCAD.  How might I go about this?

 

I believe the basic steps would be something like this:

1.  Identify which object to copy from template sheet.

2.  Save that object to memory

3.  Identify object to replace in existing drawing

4.  Copy attributes and get position of existing titleblock

5.  Delete existing titleblock

6. Insert new titleblock and copy in attributes

 

I've taken the Autocad tutorials, and I've searched for relevant posts on ThroughTheInterface.  Is there someplace else to look which might give a more in-depth example of what I'm trying to accomplish?

 

 

 

*****************************
Win7 x64 - 16gb ram
i7 3610qm
FirePro M4000

Inventor 2013
ETO 6.1
Mentor
bsee1
Posts: 208
Registered: ‎11-14-2011
Message 2 of 5 (150 Views)

Re: Swap titleblock between 2 drawings

02-06-2013 07:59 AM in reply to: bsee1

I suppose in my attempt to make a brief question, some might have assumed I wanted someone to write an entire program for me.  I don't.  I'll attempt to clarify which parts I need assistance with.

 

Right now I'm using this code to copy a titleblock from another drawing.  Is there a way to get the new objectIds of the objects that I just inserted?  Currently they are on a layer that doesn't exist.  My goal is to get the new objectID of the item(s) I just copied, then use the setLayer property.

acCurDb.WblockCloneObjects(objColl, btr.ObjectId, iMap, DuplicateRecordCloning.Ignore, false);

 

I noticed that attributes doesn't have a set command.  Is there a specific way I should copy items from an attributeCollection to a new attributeCollection?

*****************************
Win7 x64 - 16gb ram
i7 3610qm
FirePro M4000

Inventor 2013
ETO 6.1
Active Member
Posts: 8
Registered: ‎12-09-2009
Message 3 of 5 (137 Views)

Re: Swap titleblock between 2 drawings

02-07-2013 08:33 AM in reply to: bsee1

Hi,

 

I've never tried it but I assume the IdMap gives you the facility to look up the new id from the original one..

 

IdMapping

 

IdMapping acIdMap = new IdMapping();
acCurDb.WblockCloneObjects(interferenceIdCol, acBlkTblRecNewDoc.ObjectId, acIdMap,
                           DuplicateRecordCloning.Ignore, false);

newId = acIdMap.Lookup(oldId);

 

 

Setting attributes you just loop through them and set the text string (maybe there is away to do the whole collection at once but I haven't managed it - hopefully someone with more time/experience than me can confirm if it can be done)...

foreach (ObjectId attId in attcol)
{
    AttributeReference attRef = (AttributeReference)trans.GetObject(attId, OpenMode.ForWrite);
    if (attRef.Tag.ToLower() == attName.ToLower())
    {
        if (attRef.TextString.ToLower() != attValue.ToLower())
        {
            attRef.TextString = attValue;
            attUpdated += 1;
        }
    }
}

I think I would be tempted to make a list of key value pairs containing the attribute tag and the text string from the old block, then loop through the new block setting the appropriate value. 

 

 

Regards,

 

Graham

Mitek Industries

Mentor
bsee1
Posts: 208
Registered: ‎11-14-2011
Message 4 of 5 (126 Views)

Re: Swap titleblock between 2 drawings

02-07-2013 09:01 AM in reply to: bsee1

Actually, my code doesn't do what i thought it did...which means my last question was no longer relevant.  First, the code from adndevblog that I was using is wrong.  It had me copying from and to the wrong databases.  ThroughTheInterface has similar code from 2006 that is correct, so I'm using that now.

 

Apparently, this only copies the drawing definition: 

var iMap = new IdMapping();
sourceDatabase.WblockCloneObjects(objColl, bt.ObjectId, iMap, DuplicateRecordCloning.Ignore, false);

 Now that I have the definition, what do I need to do to actually insert this as a block in the drawing? (so i can see it)

 

This is what I have so far to insert a new block, but it obviously isn't complete.  If I understand correctly, the above code creates a BlockTableRecord.  For the code below, I don't know how to specify which BlockTableRecord(definition) I just inserted.

//Add a blockReference to the newly inserted blockTableRecord
BlockTableRecord newbtr = ???????
newbtr.Name = blockToReplace.Name + "1";
ObjectId newbtrId = bt.Add(newbtr);
tr.AddNewlyCreatedDBObject(newbtr, true);

 

*****************************
Win7 x64 - 16gb ram
i7 3610qm
FirePro M4000

Inventor 2013
ETO 6.1
Active Member
Posts: 8
Registered: ‎12-09-2009
Message 5 of 5 (115 Views)

Re: Swap titleblock between 2 drawings

02-07-2013 10:34 AM in reply to: bsee1

Inserting a block is something like this (where [cassName] is the name of your block...

 

            BlockTable bt = (BlockTable)trans.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForWrite);
            BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
            try
            {
                BlockTableRecord blk = (BlockTableRecord)trans.GetObject(bt[cassName], OpenMode.ForWrite);
                if (blk != null)
                {
                    Autodesk.AutoCAD.DatabaseServices.BlockReference blkRec = new Autodesk.AutoCAD.DatabaseServices.BlockReference(ip, blk.ObjectId);
                    blkRec.SetDatabaseDefaults();
                    blkRec.LayerId = layId;
                    btr.AppendEntity(blkRec);
                    trans.AddNewlyCreatedDBObject(blkRec, true);
                    Extents3d ext3d = (Extents3d)blkRec.Bounds;
                    ip = new Point3d(ext3d.MaxPoint.X + 300, ext3d.MaxPoint.Y, 0);
                }
            }
            catch
            {
            }

 Ignore the last couple of lines relating the Extents3D.

 

You will then need to get the attributes collection and fill out the values similar to the previous code (i think you do this even if you want the default values).

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you interested in helping shape the Autodesk Community?
We’re looking at a few different ways to improve the “All Forums” landing page and need your feedback! If interested, please take a few minutes to fill out the following Usability Study. Thank you for your time!

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community