我可以用类似于这个C ++示例的方式实例化C#类吗?

时间:2010-10-07 11:58:55

标签: c# c++ porting rhino3d

我已将以下C ++代码翻译成C#,但我无法确定实例化该类的位置和方式。如果我这样做:

customAnalysis test = new customAnalysis();

它收到一条错误消息,告诉我该类是由Rhino3D实例化的,因此我不应该实例化它。但我需要创建一个类的引用,这样我就可以获得由基类创建的静态成员m_am_id

这是我遇到问题的路线:

static class CZAnalysisVAM theZAnalysisVAM;

提前致谢。

C ++代码:

//////////////////////////////////////////////////////////////////
//
// BEGIN Z analysis mode class
// 

// This is an example that demonstrates how to add a false color
// analysis mode to Rhino.  This example uses false color to indicate
// the world "z" coordinate.

// {DF4688C-9671-4389-AC41-515B8693A783}
static const GUID FALSE_COLOR_EXAMPLE_ANALYSIS_MODE_ID =
{ 0xDF4688C, 0x9671, 0x4389, { 0xAC, 0x41, 0x51, 0x5B, 0x86, 0x93, 0xA7, 0x83 } };

class CZAnalysisVAM : public CRhinoVisualAnalysisMode
{
public:
  CZAnalysisVAM();
  ~CZAnalysisVAM();

  // virtual CRhinoVisualAnalysisMode override
  void GetAnalysisModeName( ON_wString& name ) const;

  // virtual CRhinoVisualAnalysisMode override
  bool ObjectSupportsAnalysisMode( const CRhinoObject* object ) const;

  // virtual CRhinoVisualAnalysisMode override
  void UpdateVertexColors( 
            const CRhinoObject* object, 
            ON_SimpleArray<const ON_Mesh *>& meshes 
            ) const;

  // virtual CRhinoVisualAnalysisMode override
  bool ShowIsoCurves() const;

  // virtual CRhinoVisualAnalysisMode override
  void DrawMeshObject( 
          const CRhinoMeshObject& mesh_object,
          CRhinoDisplayPipeline& dp
          );

  // virtual CRhinoVisualAnalysisMode override
  void DrawBrepObject( 
          const CRhinoBrepObject& brep_object,
          CRhinoDisplayPipeline& dp
          );

  bool m_bShowIsoCurves;

  // This simple example provides a false color based on the
  // world z coordinate.   For details, see the implementation
  // of the FalseColor function.
  ON_Interval m_z_range;  
  ON_Interval m_hue_range;

  ON_Color FalseColor(double z) const;

  // Returns a mapping tag that is used to detect when
  // a meshes colors need to be set.  For details, see the
  // implementation  of MappingTag and UpdateVertexColors.
  ON_MappingTag MappingTag() const;
};


// the one and only instance of a CZAnalysisVAM object.
// The CRhinoVisualAnalysisMode constructor registers the mode
// with Rhino.  This class must not be destroyed while Rhino
// is active.
static class CZAnalysisVAM theZAnalysisVAM;

CZAnalysisVAM::CZAnalysisVAM() 
: CRhinoVisualAnalysisMode( FALSE_COLOR_EXAMPLE_ANALYSIS_MODE_ID,
                            CRhinoVisualAnalysisMode::false_color_style
                            )
{
  m_bShowIsoCurves = true;

  // In a real plug-in, user interface would allow
  // the user to change these intervals.

  m_z_range.Set(-10.0,10.0);
  m_hue_range.Set(0.0,4.0*ON_PI/3.0); // red to green to blue
}

CZAnalysisVAM::~CZAnalysisVAM()
{
}

void CZAnalysisVAM::GetAnalysisModeName( ON_wString& name ) const
{
  // This name shows up in the object properties details
  // report when the object is in the analysis mode.
  name = L"Z analysis";
}

bool CZAnalysisVAM::ObjectSupportsAnalysisMode( const CRhinoObject* object ) const
{
  // This function should return true if the analysis mode works
  // on the object.  This example works on meshes and breps, so
  // its version of ObjectSupportsAnalysisMode looks like this.

  bool rc = false;
  if ( object )
  {
    switch(object->ObjectType())
    {
    case ON::mesh_object:
      if ( CRhinoMeshObject::Cast(object) )
        rc = true;
      break;

    case ON::surface_object:
    case ON::polysrf_filter:
    case ON::brep_object:
      if ( CRhinoBrepObject::Cast(object) )
        rc = true;
      break;
    }
  }
  return rc;
}

ON_MappingTag CZAnalysisVAM::MappingTag() const
{
  ON_MappingTag mt;

  // Since the false colors that are shown will change if
  // the mesh is transformed, we have to initialize the
  // transformation.
  mt.m_mesh_xform.Identity();

  // This is the analysis mode id passed to the 
  // CRhinoVisualAnalysisMode constructor. Use the
  // m_am_id member and it this code will alwasy 
  // work correctly.
  mt.m_mapping_id = m_am_id;

  // This is a 32 bit CRC or the information used to
  // set the false colors.
  // For this example, the m_z_range and m_hue_range
  // intervals controlthe colors, so we calculate 
  // their crc.
  mt.m_mapping_crc = 0;
  mt.m_mapping_crc = ON_CRC32(mt.m_mapping_crc,sizeof(m_z_range),&m_z_range);
  mt.m_mapping_crc = ON_CRC32(mt.m_mapping_crc,sizeof(m_hue_range),&m_hue_range);

  return mt;
}

ON_Color CZAnalysisVAM::FalseColor(double z) const
{
  // Simple example of one way to change a number 
  // into a color.
  double s = m_z_range.NormalizedParameterAt(z);
  if ( s < 0.0 ) s = 0.0; else if (s > 1.0) s = 1.0;
  double hue = m_hue_range.ParameterAt(s);
  ON_Color c;
  c.SetHSV( hue, 1.0, 1.0 );
  return c;
}

void CZAnalysisVAM::UpdateVertexColors( 
            const CRhinoObject* object, 
            ON_SimpleArray<const ON_Mesh *>& meshes 
            ) const
{
  // Rhino calls this function when it is time for you
  // to set the false colors on the analysis mesh vertices.
  // For breps, there is one mesh per face.  For mesh objects,
  // there is a single mesh.
  const int count = meshes.Count();
  if (count > 0 )
  {
    // A "mapping tag" is used to determine if the colors
    // need to be set.
    ON_MappingTag mt = MappingTag();

    const ON_Mesh * const * mesh_list = meshes.Array();
    for ( int mi = 0; mi < count; mi++ )
    {
      const ON_Mesh* mesh = mesh_list[mi];
      if ( mesh && mt.Compare(mesh->m_Ctag) )
      {
        // The mesh's mapping tag is different from ours. Either
        // the mesh has no false colors, has false colors set by
        // another analysis mode, has false colors set using
        // different m_z_range[]/m_hue_range[] values, or the
        // mesh has been moved.  In any case, we need to set
        // the false colors to the ones we want.

        const int vcount = mesh->m_V.Count();
        ON_SimpleArray<ON_Color>& vertex_colors = const_cast<ON_Mesh*>(mesh)->m_C;
        vertex_colors.SetCount(0);     // in case something else had set the colors
        vertex_colors.Reserve(vcount); // for efficiency
        for (int vi = 0; vi < vcount; vi++ )
        {
          double z = mesh->m_V[vi].z;
          ON_Color c = FalseColor(z);
          vertex_colors.Append(c);
        }

        // set the mesh's color tag 
        const_cast<ON_Mesh*>(mesh)->m_Ctag = mt;
      }
    }
  }
}

bool CZAnalysisVAM::ShowIsoCurves() const
{
  // Most shaded analysis modes that work on breps have
  // the option of showing or hiding isocurves.  Run the
  // built-in Rhino ZebraAnalysis to see how Rhino handles
  // the user interface.  If controlling iso-curve visability 
  // is a feature you want to support, then provide user
  // interface to set this member variable.
  return m_bShowIsoCurves;
}

//
// END Z analysis mode class
// 
//////////////////////////////////////////////////////////////////

C#代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using RMA.Rhino;
using RMA.OpenNURBS;

namespace MyPlugIn1
{



    public class customAnalysis : MRhinoVisualAnalysisMode
    {


        static Guid FALSE_COLOR_EXAMPLE_ANALYSIS_MODE_ID = new Guid("{B0CBD23A-089B-4fb2-A61A-6DE1238E7B74}");

        public OnInterval m_z_range;
        public OnInterval m_hue_range;

        bool m_bShowIsoCurves;

        public customAnalysis():base(FALSE_COLOR_EXAMPLE_ANALYSIS_MODE_ID,IRhinoVisualAnalysisMode.analysis_style.false_color_style) 
        {
            m_bShowIsoCurves = true;

            // In a real plug-in, user interface would allow
            // the user to change these intervals.

            m_z_range.Set(-10.0, 10.0);
            m_hue_range.Set(0.0, 4.0 * Math.PI / 3.0); // red to green to blue

        }


        public override string GetAnalysisModeName()
        {
            return "kineticMode";
        }


        public override bool ObjectSupportsAnalysisMode(IRhinoObject rh_object)
        {
            // This function should return true if the analysis mode works
            // on the object.  This example works on meshes and breps, so
            // its version of ObjectSupportsAnalysisMode looks like this.
            OnObject obj = rh_object.DuplicateOnObject();

            Boolean rc = false;
            if (null != rh_object && null != obj)
            {
                switch (rh_object.ObjectType())
                {
                    case IOn.object_type.mesh_object:
                        if (null != MRhinoMeshObject.Cast(obj))

                            rc = true;
                        break;

                    case IOn.object_type.surface_object:
                    case IOn.object_type.polysrf_filter:
                    case IOn.object_type.brep_object:
                        if (null != MRhinoBrepObject.Cast(obj))

                            rc = true;
                        break;
                }
            }
            return rc;
        }

        public override void UpdateVertexColors(IRhinoObject rh_object, OnMesh[] meshes)
        {
            int count = meshes.Length;

            if (count > 0)
            {
                // A "mapping tag" is used to determine if the colors
                // need to be set.
                OnMappingTag mt = MappingTag();

                //const ON_Mesh * const * mesh_list = meshes.Array();
                for (int mi = 0; mi < count; mi++)
                {
                    OnMesh mesh = meshes[mi];
                    if (null != mesh && mt.Compare(mesh.m_Ctag) > 0)
                    {
                        // The mesh's mapping tag is different from ours. Either
                        // the mesh has no false colors, has false colors set by
                        // another analysis mode, has false colors set using
                        // different m_z_range[]/m_hue_range[] values, or the
                        // mesh has been moved.  In any case, we need to set
                        // the false colors to the ones we want.

                        int vcount = mesh.m_V.Count();

                        ArrayOnColor vertex_colors = OnMesh.Cast(mesh).m_C;

                        vertex_colors.SetCount(0);     // in case something else had set the colors
                        vertex_colors.Reserve(vcount); // for efficiency

                        for (int vi = 0; vi < vcount; vi++)
                        {
                            double z = mesh.m_V[vi].z;
                            OnColor c = FalseColor(z);
                            vertex_colors.Append(c);
                        }

                        // set the mesh's color tag 
                        mesh.m_Ctag = mt;


                    }
                }
            }
        }

        public override bool ShowIsoCurves()
        {
            return m_bShowIsoCurves;
        }

        public override void DrawMeshObject(IRhinoMeshObject mesh_object, MRhinoDisplayPipeline dp)
        {
            base.DrawMeshObject(mesh_object, dp);
        }

        public override void DrawBrepObject(IRhinoBrepObject brep_object, MRhinoDisplayPipeline dp)
        {
            base.DrawBrepObject(brep_object, dp);
        }

        public OnMappingTag MappingTag()
        {

            OnMappingTag mt = null;

            // Since the false colors that are shown will change if
            // the mesh is transformed, we have to initialize the
            // transformation.
            mt.m_mesh_xform.Identity();

            // This is the analysis mode id passed to the 
            // CRhinoVisualAnalysisMode constructor. Use the
            // m_am_id member and it this code will alwasy 
            // work correctly.
            mt.m_mapping_id = m_am_id;

            // This is a 32 bit CRC or the information used to
            // set the false colors.
            // For this example, the m_z_range and m_hue_range
            // intervals control the colors, so we calculate 
            // their crc.
            mt.m_mapping_crc = 0;
            //mt.m_mapping_crc = ON_CRC32(mt.m_mapping_crc, m_z_range.Length, m_z_range);
            //mt.m_mapping_crc = ON_CRC32(mt.m_mapping_crc, m_hue_range.Length, m_hue_range);

           OnTextureMapping thomas = new OnTextureMapping();


            return mt;

        }


        public OnColor FalseColor(double z)
        {

            double s = m_z_range.NormalizedParameterAt(z);

            if (s < 0.0) s = 0.0; else if (s > 1.0) s = 1.0;

            double hue = m_hue_range.ParameterAt(s);

            OnColor c = new OnColor();

            c.SetHSV(hue, 1.0, 1.0);

            return c;

        }


        ~customAnalysis() { }

    }
}

1 个答案:

答案 0 :(得分:3)

如果您需要C#中customAnalysis类的静态成员,则可以customAnalysis.m_am_id访问它。由于该成员是静态的,因此您可以使用类本身访问它,而不是该类的对象。