1

如何循环访问 BlockReference 的 AttributeReference 并创建 AttributeReference 作为 DBText 实体的精确副本(对于 AutoCAD 用户来说看起来相同)。我已经从快速工具 Burst.lsp 中看到了在 AutoLisp 中执行此操作的代码,但我无法弄清楚如何将其转换为 .Net。这是循环遍历属性引用的简单代码

    foreach (ObjectId attributeReferenceId in blockReference.AttributeCollection)
    {
      AttributeReference attributeReference = (AttributeReference)transaction.GetObject(attributeReferenceId, OpenMode.ForWrite);
      // Make DBText copy
    }
4

2 回答 2

2

为了解决这个问题,我将特定属性从 复制AttributeReference到一个新DBText对象中。这是对我有用的特定代码来破坏块引用。 SetTextStyle并且GetTextStyleDBText类的扩展方法,用于处理 AutoCAD API 版本之间的更改。

private static void BurstSingle(Transaction transaction, Database database, 
 BlockReference blockReference)
{
  BlockTableRecord theModelSpaceBlock = 
 (BlockTableRecord)transaction.GetObject(blockReference.BlockId, OpenMode.ForWrite);

  foreach (ObjectId attributeReferenceId in blockReference.AttributeCollection)
  {
 AttributeReference attributeReference = 
  (AttributeReference)transaction.GetObject(attributeReferenceId, OpenMode.ForWrite);

 Entity textVersionOfAttributeReference;

 if (attributeReference.IsMTextAttribute)
 {
   MText mText = (MText)attributeReference.MTextAttribute.Clone();
   textVersionOfAttributeReference = mText;
 }
 else
 {
   DBText dbText = new DBText();
   dbText.SetDatabaseDefaults(database);
   dbText.Thickness = attributeReference.Thickness;

   if (attributeReference.LayerId == database.LayerZero)
  dbText.LayerId = blockReference.LayerId;
   else
  dbText.LayerId = attributeReference.LayerId;

   if (attributeReference.ColorIndex == EntityColorIndex_ByBlock)
  dbText.ColorIndex = blockReference.ColorIndex;
   else
  dbText.ColorIndex = attributeReference.ColorIndex;

   if (attributeReference.Linetype.ToUpper() == "BYBLOCK")
  dbText.LinetypeId = blockReference.LinetypeId;
   else
  dbText.LinetypeId = attributeReference.LinetypeId;

   dbText.Height = attributeReference.Height;
   dbText.TextString = attributeReference.TextString;
   dbText.Rotation = attributeReference.Rotation;
   dbText.Oblique = attributeReference.Oblique;
   dbText.SetTextStyle(attributeReference.GetTextStyle());
   dbText.IsMirroredInX = attributeReference.IsMirroredInX;
   dbText.IsMirroredInY = attributeReference.IsMirroredInY;
   dbText.HorizontalMode = attributeReference.HorizontalMode;
   dbText.VerticalMode = attributeReference.VerticalMode;

   if (attributeReference.AlignmentPoint.Y != 0.0)
  dbText.AlignmentPoint = attributeReference.AlignmentPoint;

   dbText.Position = attributeReference.Position;
   dbText.Normal = attributeReference.Normal;
   dbText.WidthFactor = attributeReference.WidthFactor;

   textVersionOfAttributeReference = dbText;

   theModelSpaceBlock.AppendEntity(textVersionOfAttributeReference);
   transaction.AddNewlyCreatedDBObject(textVersionOfAttributeReference, true);
 }
  }

  DBObjectCollection explodedParts = new DBObjectCollection();
  blockReference.Explode(explodedParts);

  foreach (Entity explodedPart in explodedParts)
  {
 if (!(explodedPart is AttributeDefinition))
 {
   if (explodedPart.ColorIndex == EntityColorIndex_ByBlock)
  explodedPart.ColorIndex = blockReference.ColorIndex;

   if (explodedPart.LayerId == database.LayerZero)
  explodedPart.LayerId = blockReference.LayerId;

   if (explodedPart.Linetype.ToUpper() == "BYBLOCK")
  explodedPart.LinetypeId = blockReference.LinetypeId;

   theModelSpaceBlock.AppendEntity(explodedPart);
   transaction.AddNewlyCreatedDBObject(explodedPart, true);
 }

 explodedPart.Dispose();
  }

  explodedParts.Clear();

  blockReference.Erase();
}
于 2010-12-22T20:49:04.130 回答
0

AttributeReference对象同时支持CloneDeepClone函数。你尝试过其中任何一个吗?

于 2010-09-27T11:34:20.173 回答