本帖最后由 carrot1983 于 2011-5-6 15:22 编辑
简单写了个LISP的例子,如例子中的 pt 变量。
(setq pt (polar pt 0 20))
- (defun c:tt ()
- (setq pt (getpoint "\n指定插入点: "))
- (repeat 10
- (setq pt (polar pt 0 20))
- (command "._CIRCLE" "_NON" pt 5)
- )
- (princ)
- )
想要实现在循环语句中,变量重复利用,但是在C#下面,一直没开窍。
错误提示:
不能在此范围内声明名为“insertPoint”的局部变量,因为这样会使“insertPoint”具有不同的含义,而它已在“父级或当前”范围中表示其他内容了 (CS0136) - D:\Sharp\work\cs20110505a\cs20110505a\MyClass.cs:62,13
C#代码如下:
PromptPointResult pPtRes;
PromptPointOptions pPtOpts = new PromptPointOptions("");
// 提示开始点
pPtOpts.Message = "\n指定插入点: ";
pPtRes = doc.Editor.GetPoint(pPtOpts);
Point3d insertPoint = pPtRes.Value;
// 如果用户按了 ESC 键或取消了命令就退出
if (pPtRes.Status == PromptStatus.Cancel) return;
for (int i = 0; i < 10;i++)
{
Point3d insertPoint = PolarPoint(insertPoint,0,20); //这里出错了。。。如何修改才是对的。
ObjectId entId = AddCircle(insertPoint, 5);
}
// Polar
public static Point3d PolarPoint(Point3d pPt, double dAng, double dDist)
{
return new Point3d(pPt.X + dDist * Math.Cos(dAng),
pPt.Y + dDist * Math.Sin(dAng),
pPt.Z);
}
// 将图形对象加入到模型空间的函数.
public static ObjectId AppendEntity(Entity ent)
{
Database db = HostApplicationServices.WorkingDatabase;
ObjectId entId;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
entId = btr.AppendEntity(ent);
trans.AddNewlyCreatedDBObject(ent, true);
trans.Commit();
}
return entId;
}
// 由圆心和半径创建圆的函数.
public static ObjectId AddCircle(Point3d cenPt, double radius)
{
Circle ent = new Circle(cenPt, Vector3d.ZAxis, radius);
ObjectId entId = AppendEntity(ent);
return entId;
}
|