请教高手:c#创建块出问题了
本帖最后由 作者 于 2009-12-30 16:45:10 编辑 <br /><br /> <p>c#创建块出问题:</p><p>我电脑中装的是VS2008,AUTOCAD2008;创建块是提示“绑定到方法时出错”;详图见附件;</p><p>请高手看看怎么回事,谢谢!!!!!!</p> 不好意思,刚才忘记上传程序代码了。 <p>不要返回值,改一个,其余类似</p><p> <br/> public void CreateBlock()<br/> {<br/> Database db = HostApplicationServices.WorkingDatabase;<br/> ObjectId blockId = ObjectId.Null;//用于返回所创建的块的对象Id<br/> BlockTableRecord record = new BlockTableRecord();//创建一个BlockTableRecord类的对象,表示所要创建的块<br/> record.Name = "Room";//设置块名 <br/> using (Transaction trans = db.TransactionManager.StartTransaction())<br/> {<br/> Point3dCollection points = new Point3dCollection();//用于表示组成块的多段线的顶点<br/> points.Add(new Point3d(-18.0, -6.0, 0.0));<br/> points.Add(new Point3d(18.0, -6.0, 0.0));<br/> points.Add(new Point3d(18.0, 6.0, 0.0));<br/> points.Add(new Point3d(-18.0, 6.0, 0.0));<br/> Polyline2d pline = new Polyline2d(Poly2dType.SimplePoly, points, 0.0, true, 0.0, 0.0, null);//创建组成块的多段线<br/> record.Origin = points;//设置块的基点<br/> record.AppendEntity(pline);//将多段线加入到新建的BlockTableRecord对象<br/> BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);//以写的方式打开块表<br/> if (!bt.Has("Room"))//判断是否存在名为"Room"的块<br/> {<br/> blockId = bt.Add(record);//在块表中加入"Room"块<br/> trans.AddNewlyCreatedDBObject(record, true);//通知事务处理<br/> trans.Commit();//提交事务<br/> }<br/> }<br/> //return blockId;//返回创建的块的对象Id<br/> }<br/></p> <p>不要返回值,调试时autocad2008没有创建任何块吗?怎末回事?</p><p>使用上面的代码调试,cad屏幕无任何图形</p> <p>把块定义和块参照分清楚先</p><p>你只定义了一个块,当然没有显示</p> <p>谢谢超级版主雪山飞狐;我刚刚接触C#二次开发cad;您说的有道理,谢谢赐教!!!</p><p></p> 雪山飞狐_lzh 发表于 2009-12-30 18:56 static/image/common/back.gif不要返回值,改一个,其余类似 &n ...
飞狐,您好,请问一下如果是创建含填充色的块,改如何创建,下面是代码,到acTrans.AddNewlyCreatedDBObject(acCir, true);时报错
//创建块定义
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
BlockTable bt = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite) as BlockTable;
BlockTableRecord blk = null;
if (!bt.Has("美化天线"))
{
blk = new BlockTableRecord();
blk.Name = "美化天线";
Circle acCir = new Circle();
acCir.Radius = 1.5;
acCir.Center = new Point3d(0, 0, 0);
blk.AppendEntity(acCir);
//acTrans.AddNewlyCreatedDBObject(acCir, true);
Circle acCir1 = new Circle();
acCir1.Radius = 3;
acCir1.Center = new Point3d(0, 0, 0);
blk.AppendEntity(acCir1);
//acTrans.AddNewlyCreatedDBObject(acCir1, true);
/*
ObjectIdCollection acObjIdColl = new ObjectIdCollection();
acObjIdColl.Add(acCir.ObjectId);
ObjectIdCollection acObjIdColl1 = new ObjectIdCollection();
acObjIdColl1.Add(acCir1.ObjectId);
Hatch acHatch = new Hatch();
acTrans.AddNewlyCreatedDBObject(acHatch, true);
acHatch.SetDatabaseDefaults();
acHatch.HatchObjectType = HatchObjectType.GradientObject;
acHatch.SetGradient(GradientPatternType.PreDefinedGradient, "SPHERICAL");
// We're defining two colours
acHatch.GradientOneColorMode = false;
GradientColor[] gcs = new GradientColor;
gcs = new GradientColor(Color.FromRgb(213, 213, 210), 0);
gcs = new GradientColor(Color.FromRgb(255, 255, 255), 1);
acHatch.SetGradientColors(gcs);
//acHatch.Elevation = 0;
acHatch.Associative = true;
acHatch.AppendLoop(HatchLoopTypes.Outermost, acObjIdColl);
acHatch.AppendLoop(HatchLoopTypes.Default, acObjIdColl1);
acHatch.EvaluateHatch(true);
acHatch.HatchStyle = HatchStyle.Outer;
blk.AppendEntity(acHatch);
*/
labelblkid = bt.Add(blk);
acTrans.AddNewlyCreatedDBObject(blk, true);
}
else
{
blk = acTrans.GetObject(bt["美化天线"], OpenMode.ForWrite) as BlockTableRecord;
labelblkid = blk.ObjectId;
}
acTrans.Commit();
}
页:
[1]