Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将AttributeReference转换为DBText_C#_.net_Autocad - Fatal编程技术网

C# 将AttributeReference转换为DBText

C# 将AttributeReference转换为DBText,c#,.net,autocad,C#,.net,Autocad,如何循环块引用的AttributeReference,并将AttributeReference创建为DBText图元的精确副本(在AutoCAD用户看来是相同的)。我在express tools burst.lsp中看到了在AutoLisp中执行此操作的代码,但我还没有弄清楚如何将其转换为.Net。下面是循环遍历属性引用的简单代码 foreach (ObjectId attributeReferenceId in blockReference.AttributeCollection)

如何循环块引用的AttributeReference,并将AttributeReference创建为DBText图元的精确副本(在AutoCAD用户看来是相同的)。我在express tools burst.lsp中看到了在AutoLisp中执行此操作的代码,但我还没有弄清楚如何将其转换为.Net。下面是循环遍历属性引用的简单代码

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

AttributeReference
对象支持
Clone
DeepClone
功能。您尝试过其中任何一个吗?

为了解决此问题,我已将
属性引用
中的特定属性复制到一个新的
DBText
对象中。下面是一段特定的代码,它为我提供了一个blockreference
SetTextStyle
GetTextStyle
DBText
类上的扩展方法,用于处理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();
}

调用这些函数只会创建一个新的AttributeReference。我需要一个DBText对象。我明白了。我忽略了DBText要求。对不起。