TheSwamp
Code Red => .NET => Topic started by: latour_g on January 07, 2014, 03:20:07 PM
-
Hi,
This function is to copy block. First, you select the blocks that you want to replace and after you select the block to be copied. The only thing that don't work like I want is the position of the attributes, I can't set the new position (see at the end of the code). If anyone can help me on that ... I would appreciate :-)
private void btnCopyBlk_Click(object sender, EventArgs e)
{
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
//
TypedValue[] values = new TypedValue[] { new TypedValue((int)DxfCode.Start, "INSERT") };
SelectionFilter filter = new SelectionFilter(values);
PromptSelectionOptions SelOpts = new PromptSelectionOptions();
SelOpts.AllowDuplicates = false;
SelOpts.MessageForAdding = "Select objects: ";
SelOpts.MessageForRemoval = "Select objects to remove";
Utils.SetFocusToDwgView();
PromptSelectionResult res = ed.GetSelection(SelOpts, filter);
if (res.Status != PromptStatus.OK) return;
//
PromptEntityOptions peo = new PromptEntityOptions("Select block");
peo.SetRejectMessage("No block selected");
peo.AllowNone = false;
peo.AddAllowedClass(typeof(BlockReference), false);
PromptEntityResult per = ed.GetEntity(peo);
// abandon si la sélection n'a pas été faite
if (per.Status != PromptStatus.OK) return;
//
DocumentLock loc = doc.LockDocument();
using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
// detail du bloc de remplacement
BlockReference blkR = (BlockReference)tr.GetObject(per.ObjectId, OpenMode.ForRead);
BlockTableRecord btNr = (BlockTableRecord)tr.GetObject(bt[blkR.Name], OpenMode.ForRead);
//
SelectionSet ss = res.Value;
ObjectId[] idArray = ss.GetObjectIds();
foreach (ObjectId id in idArray)
{
SortedList<string, string> attLst = new SortedList<string, string>();
SortedList<string, AttributeReference> attAR = new SortedList<string, AttributeReference>();
BlockReference blk = (BlockReference)tr.GetObject(id, OpenMode.ForWrite);
// il y a des attributs dans le block à remplacer
// les effacer
foreach (ObjectId attId in blk.AttributeCollection)
{
if (attId.IsErased) continue;
AttributeReference att = (AttributeReference)tr.GetObject(attId, OpenMode.ForWrite);
att.Erase(true);
}
// il y a des attributs dans le block de remplacement
// lire les valeurs associées aux tag
foreach (ObjectId attId in blkR.AttributeCollection)
{
AttributeReference att = (AttributeReference)tr.GetObject(attId, OpenMode.ForRead);
if (!attLst.ContainsKey(att.Tag))
{
attLst.Add(att.Tag, att.TextString);
attAR.Add(att.Tag, att);
}
}
blk.BlockTableRecord = btNr.ObjectId;
blk.Layer = blkR.Layer;
// il y a des attributs dans le bloc de remplacement
if (btNr.HasAttributeDefinitions)
{
// ajout des attributs a la reference du block
IEnumerator Ie = btNr.GetEnumerator();
while (Ie.MoveNext())
{
try
{
DBObject obj = tr.GetObject((ObjectId)Ie.Current, OpenMode.ForRead, false);
if (obj.GetType() == typeof(AttributeDefinition))
{
AttributeDefinition attDef = (AttributeDefinition)obj;
//Create a new attribute reference
AttributeReference attRef = new AttributeReference();
AttributeReference attRefCopie = new AttributeReference();
if (attLst.ContainsKey(attDef.Tag)) attRef.TextString = attLst[attDef.Tag];
if (attLst.ContainsKey(attDef.Tag)) attRefCopie = attAR[attDef.Tag];
attRef.SetDatabaseDefaults();
attRef.SetAttributeFromBlock(attDef, blk.BlockTransform);
attRef.Layer = attRefCopie.Layer;
attRef.TextString = attRefCopie.TextString;
attRef.Rotation = attRefCopie.Rotation+blk.Rotation;
attRef.Color = attRefCopie.Color;
attRef.TextStyleId = attRefCopie.TextStyleId;
attRef.Justify = attRefCopie.Justify;
// --------------------------
// Not working
attRef.Position = attDef.Position.TransformBy(blk.BlockTransform);
// --------------------------
// --------------------------
//Add the attribute reference to the block ref
blk.AttributeCollection.AppendAttribute(attRef);
tr.AddNewlyCreatedDBObject(attRef, true);
}
}
catch (System.Exception ex) { MessageBox.Show(ex.ToString()); }
}
}
}
tr.Commit();
}
loc.Dispose();
Utils.PostCommandPrompt();
}
-
On the quick glance
Set attref.position=...
before of
attref.Justify=...
then add this line:
attref.AdjustAlignment(db);
-
Still not working :|
attRef.Position = attDef.Position.TransformBy(blk.BlockTransform);
attRef.Justify = attRefCopie.Justify;
attRef.AdjustAlignment(db);