当前位置: 代码迷 >> AutoCAD >> cad2008用c#二次开发的填充颜色的有关问题
  详细解决方案

cad2008用c#二次开发的填充颜色的有关问题

热度:6217   发布时间:2013-02-26 00:00:00.0
cad2008用c#二次开发的填充颜色的问题
cad2008用c#二次开发的填充颜色的问题,下面的代码画一个圆填充颜色没问题,我想画几根线组合的封闭图形(就是几个对象的封闭图),再填充颜色,该怎么写呢?
  Database db = HostApplicationServices.WorkingDatabase;
  Transaction trans = db.TransactionManager.StartTransaction();
  try
  {
  Circle circle = new Circle(new Point3d(10, 10, 0), Vector3d.ZAxis, 200);
  BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
  BlockTableRecord btr = (BlockTableRecord)trans.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId, OpenMode.ForWrite);
  btr.AppendEntity(circle);
   
   

  trans.AddNewlyCreatedDBObject(circle, true);
  ObjectIdCollection collection = new ObjectIdCollection();
  collection.Add(circle.ObjectId);
  // collection.Add(circle1.ObjectId);

   
  Hatch hatch = new Hatch();

  hatch.Elevation = 0;
  hatch.HatchStyle = HatchStyle.Normal;
  hatch.ColorIndex = 10;
  hatch.PatternAngle = 0;
  hatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID"); //设置填充图案 
  // hatch.Associative = true;
  hatch.AppendLoop(HatchLoopTypes.Default, collection); //设置填充边界 //
  hatch.EvaluateHatch(true);
  btr.AppendEntity(hatch);
  trans.AddNewlyCreatedDBObject(hatch, true);
  trans.Commit();
  }
  catch
  {
  ed.WriteMessage("Error ");
  }
  finally
  {
  trans.Dispose();
  }


------解决方案--------------------------------------------------------
用Polyline可以实现:
[CommandMethod("MyDraw")]
static public void DoDraw()
{
Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction();
try
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId, OpenMode.ForWrite);


Polyline polyline = new Polyline();
polyline.SetDatabaseDefaults();
polyline.Reset(false, 0);

polyline.AddVertexAt(0, new Point2d(-10, -10), 0, 0, 0);
polyline.AddVertexAt(1, new Point2d(-10, 20), 0, 0, 0);
polyline.AddVertexAt(2, new Point2d(30, 20), 0, 0, 0);
polyline.AddVertexAt(3, new Point2d(30, -10), 0, 0, 0);
polyline.Closed = true;
btr.AppendEntity(polyline);
trans.AddNewlyCreatedDBObject(polyline, true);
ObjectIdCollection collection = new ObjectIdCollection();
collection.Add(polyline.ObjectId);

Hatch hatch = new Hatch();

hatch.Elevation = 0;
hatch.HatchStyle = HatchStyle.Normal;
hatch.ColorIndex = 10;
hatch.PatternAngle = 0;
hatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID"); //设置填充图案
  相关解决方案