https://discourse.mcneel.com/t/get-obj-from-rhinodoc-without-write-in-file/55732/3
public class TestVictorCommand : Command
{
public override string EnglishName = "TestVictor";
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
// Select some object
var go = new GetObject();
go.SetCommandPrompt("Select object");
go.SubObjectSelect = false;
go.Get();
if (go.CommandResult() != Result.Success)
return go.CommandResult();
// Get the selected RhinoObject
var rh_obj = go.Object(0).Object();
if (null == rh_obj)
return Result.Failure;
// You cannot serialize a RhinoObject. But you can serialize
// its properties.
// Convert Geometry and Attributes to bytes
var geometry_bytes = ConvertCommonObjectToBytes(rh_obj.Geometry);
var attributes_bytes = ConvertCommonObjectToBytes(rh_obj.Attributes);
if (null == geometry_bytes || null == attributes_bytes)
return Result.Failure;
// TODO...
// Create Geometry from bytes
var common_object = CreateCommonObjectFromBytes(geometry_bytes);
var new_geometry = common_object as GeometryBase;
// Create Attributes from bytes
common_object = CreateCommonObjectFromBytes(attributes_bytes);
var new_attribues = common_object as ObjectAttributes;
// Validate
if (null == new_geometry || !new_geometry.IsValid || null == new_attribues || !new_attribues.IsValid)
return Result.Failure;
new_attribues.ObjectId = Guid.Empty;
doc.Objects.Add(new_geometry, new_attribues);
doc.Views.Redraw();
return Result.Success;
}
public static byte[] ConvertCommonObjectToBytes(CommonObject src)
{
if (null == src)
return null;
byte[] rc = null;
try
{
var formatter = new BinaryFormatter();
using (var stream = new MemoryStream())
{
formatter.Serialize(stream, src);
rc = stream.ToArray();
}
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
return rc;
}
public static CommonObject CreateCommonObjectFromBytes(byte[] bytes)
{
if (null == bytes || 0 == bytes.Length)
return null;
CommonObject rc = null;
try
{
using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
stream.Write(bytes, 0, bytes.Length);
stream.Seek(0, SeekOrigin.Begin);
var obj = formatter.Deserialize(stream) as CommonObject;
if (null != obj)
rc = obj;
}
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
return rc;
}
}
meshObjRefs = Rhino.DocObjects.RhinoObject.GetRenderMeshes(doc.Objects.GetObjectList(object_enumerator_settings), true, false);
if (null != meshObjRefs)
{
for (int i = 0; i < meshObjRefs.Length; i++)
{
Mesh mesh = meshObjRefs[i].Mesh();
foreach (Rhino.Geometry.Point3f point in mesh.Vertices)
{
s.Append("v ");
s.Append(point.X.ToString(_allFloatDigits));
s.Append(" ");
s.Append(point.Y.ToString(_allFloatDigits));
s.Append(" ");
s.Append(point.Z.ToString(_allFloatDigits));
s.Append(System.Environment.NewLine);
}
vertex = mesh.Vertices.Count;
foreach (Rhino.Geometry.Point2f point in mesh.TextureCoordinates)
{
s.Append("vt ");
s.Append(point.X.ToString(_allFloatDigits));
s.Append(" ");
s.Append(point.Y.ToString(_allFloatDigits));
s.Append(System.Environment.NewLine);
}
foreach (Rhino.Geometry.Vector3f point in mesh.Normals)
{
s.Append("vn ");
s.Append(point.X.ToString(_allFloatDigits));
s.Append(" ");
s.Append(point.Y.ToString(_allFloatDigits));
s.Append(" ");
s.Append(point.Z.ToString(_allFloatDigits));
s.Append(System.Environment.NewLine);
}
foreach (Rhino.Geometry.MeshFace face in mesh.Faces)
{
A = face.A + shift;
B = face.B + shift;
C = face.C + shift;
s.Append("f ");
s.Append(A);
s.Append("/");
s.Append(A);
s.Append("/");
s.Append(A);
s.Append(" ");
s.Append(B);
s.Append("/");
s.Append(B);
s.Append("/");
s.Append(B);
s.Append(" ");
s.Append(C);
s.Append("/");
s.Append(C);
s.Append("/");
s.Append(C);
if (face.IsQuad)
{
D = face.D + shift;
s.Append(" ");
s.Append(D);
s.Append("/");
s.Append(D);
s.Append("/");
s.Append(D);
}
s.Append(System.Environment.NewLine);
}
foreach (Rhino.Geometry.Vector3f point in mesh.FaceNormals)
{
s.Append("#fn ");
s.Append(point.X.ToString(_allFloatDigits));
s.Append(" ");
s.Append(point.Y.ToString(_allFloatDigits));
s.Append(" ");
s.Append(point.Z.ToString(_allFloatDigits));
s.Append(System.Environment.NewLine);
}
foreach (Rhino.Geometry.Point3d point in mesh.TopologyVertices)
{
s.Append("#tv ");
s.Append(point.X.ToString(_allFloatDigits));
s.Append(" ");
s.Append(point.Y.ToString(_allFloatDigits));
s.Append(" ");
s.Append(point.Z.ToString(_allFloatDigits));
s.Append(System.Environment.NewLine);
}
shift += vertex;
}
}
网友评论