MuZzzlen 2024-04-18 15:04 采纳率: 0%
浏览 17
已结题

在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示

请问,我使用DrawViewportWires 之后,就算禁用了该电池,预览样式仍然在rhino空间可见,怎么办呀


  private void RunScript(List<Curve> crvs, System.Drawing.Color color, int thickness, ref object A)
  {


    cc = crvs;
    co = color;
    thic = thickness;

  }

  // <Custom additional code> 

  List<Curve> cc;
  Color co;
  int thic;

  //Return a BoundingBox that contains all the geometry you are about to draw.
  public override BoundingBox ClippingBox
  {
    get
    {
      return BoundingBox.Empty;
    }
  }

  //Draw all meshes in this method.


  //Draw all wires and points in this method.
  public override void DrawViewportWires(IGH_PreviewArgs args)
  {
    foreach(Curve curve in cc) { args.Display.DrawCurve(curve, co, thic); }

  }

在c# script里

img

  • 写回答

12条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-04-18 15:04
    关注

    引自免费微信小程序:皆我百晓生

    在您的C# Script中,您已经正确实现了DrawViewportWires方法来绘制曲线,但遇到的问题是即使禁用了该电池,预览样式仍然在Rhino空间中可见。为了解决这个问题,您需要确保在电池禁用时,DrawViewportWires方法不再进行任何绘制操作。以下是修改后的代码实现,以及对相关问题的详细解答:

    修改后的代码:

    using Grasshopper.Kernel;
    
    public class MyComponent : GH_Component
    {
        private List<Curve> cc;
        private Color co;
        private int thic;
    
        public MyComponent()
          : base("MyComponent", "MyComp", "Description of your component", "Category", "Subcategory")
        {
        }
    
        protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddCurveParameter("Curves", "crvs", "Input curves", GH_ParamAccess.list);
            pManager.AddColourParameter("Color", "C", "Curve color", GH_ParamAccess.item, System.Drawing.Color.Black);
            pManager.AddIntegerParameter("Thickness", "T", "Curve thickness", GH_ParamAccess.item, 1);
        }
    
        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            // Add output parameters if necessary
        }
    
        private bool _isComponentEnabled = true;
    
        public override void AddedToDocument(GH_Document document)
        {
            base.AddedToDocument(document);
            document.ObjectDisabled += Document_ObjectDisabled;
            document.ObjectEnabled += Document_ObjectEnabled;
        }
    
        private void Document_ObjectDisabled(GH_Document sender, GH_ObjectEventArgs e)
        {
            if (e.Object == this)
                _isComponentEnabled = false;
        }
    
        private void Document_ObjectEnabled(GH_Document sender, GH_ObjectEventArgs e)
        {
            if (e.Object == this)
                _isComponentEnabled = true;
        }
    
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            List<Curve> crvs = new List<Curve>();
            System.Drawing.Color color = System.Drawing.Color.Black;
            int thickness = 1;
    
            if (!DA.GetDataList(0, crvs)) return;
            if (!DA.GetData(1, ref color)) return;
            if (!DA.GetData(2, ref thickness)) return;
    
            cc = crvs;
            co = color;
            thic = thickness;
        }
    
        public override BoundingBox ClippingBox
        {
            get
            {
                if (_isComponentEnabled && cc != null)
                {
                    BoundingBox box = BoundingBox.Empty;
                    foreach (Curve curve in cc)
                        box.Union(curve.GetBoundingBox(true));
                    return box;
                }
                else
                {
                    return BoundingBox.Empty;
                }
            }
        }
    
        public override void DrawViewportWires(IGH_PreviewArgs args)
        {
            if (_isComponentEnabled && cc != null)
            {
                foreach (Curve curve in cc)
                    args.Display.DrawCurve(curve, co, thic);
            }
        }
    }
    

    详细解答:

    1. 添加状态变量:首先,在组件类中添加一个布尔型变量 _isComponentEnabled,用于跟踪当前组件是否处于启用状态。

    2. 监听文档对象状态变化:在 AddedToDocument 方法中,注册两个事件处理器:document.ObjectDisableddocument.ObjectEnabled。当组件被禁用或重新启用时,这些处理器会更新 _isComponentEnabled 的值。

    3. 使用状态变量控制绘制:在 ClippingBoxDrawViewportWires 方法中,根据 _isComponentEnabled 的值决定是否进行计算和绘制。当组件被禁用时,返回空的 BoundingBox 并且不执行任何绘制操作。当组件启用时,按照原有逻辑计算包围盒和绘制曲线。

    通过上述修改,您的组件在被禁用时将不再影响Rhino视口的预览显示。记得替换原有的代码段为上述提供的完整代码,并根据实际情况调整输入参数、输出参数、组件名称、类别等信息。这样,当您禁用电池后,预览样式将不再在Rhino空间中可见。

    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月19日
  • 创建了问题 4月18日