2010-10-07 86 views
0

我翻譯下面的C++代碼,C#,但我不能確定在何處以及如何把類的實例。如果我用做:C#:我如何實例化一個類在C#中,像舒本C++例子

customAnalysis test = new customAnalysis(); 

它收到一個errormessage的,告訴我,類是由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() { } 

    } 
} 

回答

2

如果您需要在C#中customAnalysis類的靜態成員,您可以訪問它爲customAnalysis.m_am_id。由於該成員是靜態的,因此您可以使用類本身來訪問它,而不是該類的對象。

+0

這就是我收集的內容,但是在做這件事時說我需要對象的引用。與我有什麼關係重寫一個類,並且我需要的成員是基類的一部分? – Bildsoe 2010-10-07 12:11:29

+0

如果是這樣的話,你應該可以使用'BaseClass.StaticMemberName'來獲取它。這裏是靜態方法/成員的C#編程指南:http://msdn.microsoft.com/en-us/library/79b3xss3(VS.80).aspx – dsolimano 2010-10-07 12:13:15

+0

當我這樣做VSE返回以下錯誤:對象引用是必需的非靜態字段,方法或屬性'RMA.Rhino.MRhinoVisualAnalysisMode.m_am_id.get' – Bildsoe 2010-10-07 12:35:35