Rhino对象的ObjectAttributes中存储着对象的属性,我们用Rhino.Input.Custom中的GetObject()获取对象。
// 选一些点组成平面。
GetObject get = new GetObject();
get.SetCommandPrompt("选择一个对象");
get.Get();
if (get.CommandResult() == Result.Success)
{
Object object = get.Object(0).Object() ;
ObjectAttributes objectAttributes = object.Attributes;
// 这里继续你的代码
}
else
{
RhinoApp.WriteLine("未选择对象!");
}
获取get的属性Object(),得到RhinoObject,再获取RhinoObject的属性Attributes,得到ObjectAttributes。
objectAttributes内保存着对象的图层、材质、颜色、用户字典等的信息。
这里我们以修改objectAttributes内的图层为例:
rhino文件中有图层01和图层02,我们的object此时在图层01中,我们将objectAttributes内的LayerIndex值设为2(其中:“默认图层”索引为0,“图层 01”索引为1,“图层 02”索引为2)。
objectAttributes.LayerIndex = 2;
最后将修改后的属性保存到rhino对象中。
RhinoDoc.ActiveDoc.Objects.FindId(g).Attributes = objectAttributes;
完整代码如下:
GetObject get = new GetObject();
get.SetCommandPrompt("选择一个对象");
get.Get();
if (get.CommandResult() == Result.Success)
{
Object object = get.Object(0).Object() ;
ObjectAttributes objectAttributes = object.Attributes;
// 将图层修改为“图层 02”。
objectAttributes.LayerIndex = 2;
// 将修改的属性保存到对象。
RhinoDoc.ActiveDoc.Objects.FindId(get.Object(0).Id).Attributes = objectAttributes;
// 这里继续你的代码
}
else
{
RhinoApp.WriteLine("未选择对象!");
}
其他的属性也是按照这样的思路去修改,有些属性在对象中不一定会有默认值,那也就直接在objectAttributes中设置就好,但是要注意的是,尽量不要用创建新的ObjectAttributes空类,我们用继承原有属性上修改,再保存,避免在修改过程中遗漏其他属性。