.net的自定义实体——规则重定义 - 精华帖集合
www.dimcax.com
.net的自定义实体——规则重定义
, ,
autocad2010出来了,它给.net带来的最大的一个亮点就是规则重定义(overrule),用来实现c++中的自定义对象的功能。
规则重定义说的白一点就是让直线不成为直线,圆不成圆。
下面是一个简单的规则重定义的例子,原例子来自于kean博客上的一篇f#的例子,我改写成了c#的。
public class drawoverrule: drawableoverrule
{
static drawoverrule theoverrule = new drawoverrule();
static double radius = 0.5;
sweepoptions sweepopts = new sweepoptions();
public override bool worlddraw(drawable drawable, worlddraw wd)
{
//如果为直线,则进行规则重定义来改变直线的外形
if (drawable is line)
{
line line = drawable as line;
//绘制规则重定义过的属性,由下面的setattributes函数进行控制
base.worlddraw(line, wd);
if (!line.id.isnull && line.length>0.0)
{
//在直线的周围绘制一管线
var c = wd.subentitytraits.truecolor;
wd.subentitytraits.truecolor = new entitycolor(0x00afafff);
wd.subentitytraits.lineweight = lineweight.lineweight000;
var cir = new circle(line.startpoint, line.endpoint - line.startpoint, drawoverrule.radius);
var pipe = new extrudedsurface();
pipe.createextrudedsurface(cir, line.endpoint - line.startpoint, sweepopts);
cir.dispose();
pipe.worlddraw(wd);
pipe.dispose();
wd.subentitytraits.truecolor = c;
}
return true;
}
//如果为圆,则进行规则重定义来改变圆的外形
else if(drawable is circle)
{
circle circle = drawable as circle;
base.worlddraw(circle, wd);
//需要防止错误形式的扫琼面
if (circle.radius > drawoverrule.radius)
{
//在圆的周围绘制一管线
var c = wd.subentitytraits.truecolor;
wd.subentitytraits.truecolor = new entitycolor(0x3fffe0e0);
wd.subentitytraits.lineweight = lineweight.lineweight000;
var normal = (circle.center - circle.startpoint).crossproduct(circle.normal);
var clr = new circle(circle.startpoint, normal, drawoverrule.radius);
var pipesweptsurface = new sweptsurface();
pipesweptsurface.createsweptsurface(clr, circle, this.sweepopts);
clr.dispose();
pipesweptsurface.worlddraw(wd);
pipesweptsurface.dispose();
wd.subentitytraits.truecolor = c;
}
return true;
}
return base.worlddraw(drawable, wd);
}
//设置规则重定义对象的属性
public override int setattributes(drawable drawable, drawabletraits traits)
{
var b = base.setattributes(drawable, traits);
//如果是直线,则设置颜色索引值为6,线宽为0.40mm
if (drawable is line)
{
traits.color = 6;
traits.lineweight = lineweight.lineweight040;
}
//如果是圆,则设置颜色索引值为2,线宽为0.60mm
else if (drawable is circle)
{
traits.color = 2;
traits.lineweight = lineweight.lineweight060;
}
return b;
}
[commandmethod("overrule1")]
public void overrulestart()
{
try
{
objectoverrule.addoverrule(rxclass.getclass(typeof(drawable)), drawoverrule.theoverrule, true);
overrule.overruling = true;
}
catch (system.exception e)
{
}
}
[commandmethod("overrule0")]
public void overruleend()
{
overrule.overruling = false;
}
}
复制代码
运行程序,在命令行键入overrule1,然后随意的画直线与圆,你就会发现惊奇了:
如果是在三维状态下,则更加神奇:
顶了.f#你也会.
顶了.f#你也会.
游天居士 发表于 2009-3-28 11:24 pm
f#不会,是猜的
c#最适合开发autocad,因为它拥有vb容易的特点,却具有vc++的强大功能。
f#是啥玩意?
好文章!感谢才老大的翻译,f#实在没时间猜啦~~~
每个人都应该有一颗感恩之心!
但是在没有加载程序的autocad中打开,直线还是原来的直线,圆还是原来的圆。
每个人都应该有一颗感恩之心!
老大指点下,这段代码是不是只有在vs 2008下才可编译?"var"是c#3.0之后才加入的吧。。。
实现不了才老大的效果,不知为什么
继续研究学习cad二次开发,从中寻觅人生乐趣!
实现不了才老大的效果,不知为什么
lzx838 发表于 2009-3-30 09:07 am
要加载那个dll,再输入overrule1,然后画直线和圆就可以了。
c#最适合开发autocad,因为它拥有vb容易的特点,却具有vc++的强大功能。
终于看到啦!!
原来是我把直线画得太大了,从而看不到效果!
继续研究学习cad二次开发,从中寻觅人生乐趣!