Dynamo
Primer for v2.0
简体中文
简体中文
  • 关于
  • 简介
    • 什么是 Dynamo 以及它是如何工作的?
    • Primer 用户手册、Dynamo 社区和平台
  • Dynamo 设置
  • 用户界面
    • 工作空间
    • 库
  • 节点和导线
  • 基本节点和概念
    • 节点索引
    • 计算设计的几何图形
      • 几何图形概述
      • 向量、平面和坐标系
      • 点
      • 曲线
      • 曲面
      • 实体
      • 网格
    • 程序的构建块
      • 数据
      • 数学
      • 逻辑
      • 字符串
      • 颜色
    • 使用列表进行设计
      • 什么是列表
      • 使用列表
      • 列表的列表
      • n 维列表
    • Dynamo 中的词典
      • 什么是词典
      • 词典节点
      • 代码块中的词典
      • Revit 使用案例
  • 自定义节点和软件包
    • 自定义节点
      • 自定义节点简介
      • 创建自定义节点
      • 发布到库
    • 软件包
      • 软件包简介
      • 软件包案例研究 - Mesh Toolkit
      • 开发软件包
      • 发布软件包
      • Zero-Touch 输入
  • 适用于 Revit 的 Dynamo
    • Revit 连接
    • 选择
    • 编辑
    • 创建
    • 自定义
    • 记录
  • Dynamo for Civil 3D
    • Civil 3D 连接
    • 快速入门
    • 节点库
    • 样例工作流
      • 道路
        • 灯杆放置
      • 浇口面
        • 服务设施放置
      • 公共设施
        • 重命名结构
      • 轨道
        • 间隙包络
      • 勘测
        • 点编组管理
    • 高级主题
      • 对象绑定
      • Python 和 Civil 3D
    • Dynamo Player
    • 有用的软件包
    • 资源
  • Forma Beta 版中的 Dynamo
    • 在 Forma 中设置 Dynamo Player
    • 在 Dynamo Player 中添加和共享图形
    • 在 Dynamo Player 中运行图形
    • Dynamo 计算服务与 Desktop Dynamo 的差异
  • 在 Dynamo 中编码
    • 代码块和 DesignScript
      • 什么是代码块
      • DesignScript 语法
      • 简写
      • 函数
    • 使用 DesignScript 的几何图形
      • DesignScript 几何图形基础知识
      • 几何基本体
      • 向量数学
      • 曲线:内插和控制点
      • 平移、旋转和其他变换
      • 曲面:内插、控制点、放样、旋转
      • 几何参数化
      • 相交和修剪
      • 几何布尔
      • Python 点生成器
    • Python
      • Python 节点
      • Python 和 Revit
      • 设置自己的 Python 模板
    • 语言更改
  • 最佳做法
    • 图形策略
    • 脚本编写策略
    • 脚本参考
    • 管理您的程序
    • 在 Dynamo 中高效处理大型数据集
  • 样例工作流
    • 快速入门工作流
      • 参数化花瓶
      • 吸引器点
    • 概念索引
  • 开发人员入门
    • 从源代码构建 Dynamo
      • 从源代码构建 DynamoRevit
      • 在 Dynamo 中管理和更新依赖项
    • 为 Dynamo 开发
      • 快速入门
      • Zero-Touch 案例研究 - 网格节点
      • 在 Zero-Touch 节点中执行 Python 脚本 (C#)
      • 进一步了解 Zero-Touch
      • 高级 Dynamo 节点自定义
      • 在 Dynamo 软件包中使用 COM(互操作)类型
      • NodeModel 案例研究 - 自定义 UI
      • 更新 Dynamo 2.x 的软件包和 Dynamo 库
      • 更新 Dynamo 3.x 的软件包和 Dynamo 库
      • 扩展程序
      • 为 Dynamo 2.0+ 定义自定义软件包组织
      • Dynamo 命令行界面
      • Dynamo 集成
      • 为 Dynamo For Revit 开发
      • 发布软件包
      • 从 Visual Studio 构建软件包
      • 软件包形式的扩展
    • 拉取请求
    • 测试期望
    • 示例
  • 附录
    • 常见问题解答
    • 可视化编程和 Dynamo
    • 资源
    • 发行说明
    • 有用的软件包
    • 示例文件
    • 主体集成图
    • 下载 PDF
    • Dynamo 键盘快捷键
Powered by GitBook
On this page
  • 自定义矩形网格节点
  • 自定义节点修改
Edit on GitHub
Export as PDF
  1. 开发人员入门
  2. 为 Dynamo 开发

Zero-Touch 案例研究 - 网格节点

Previous快速入门Next在 Zero-Touch 节点中执行 Python 脚本 (C#)

Last updated 2 months ago

在 Visual Studio 项目启动并运行后,我们将介绍如何构建一个自定义节点来创建一个单元的矩形网格。尽管我们可以使用多个标准节点创建该矩形网格,但它是一个非常有用的工具,可轻松包含在 Zero-Touch 节点中。与网格线不同,单元可以围绕其中心点进行单元、查询其角顶点,或将其构建到面中。

本例将介绍创建 Zero-Touch 节点时需要注意的一些功能和概念。在我们构建自定义节点并将其添加到 Dynamo 中后,请确保查看“进一步了解 Zero-Touch”页面,以更深入地了解默认输入值、返回多个值、文档、对象、使用 Dynamo 几何类型和移植。

自定义矩形网格节点

要开始构建网格节点,请创建一个新的 Visual Studio 类库项目。有关如何设置项目的深入介绍,请参见“快速入门”页面。

  1. 选择 Class Library 作为项目类型

  2. 将项目命名为 CustomNodes

由于我们将要创建几何图形,因此我们需要参照相应的 NuGet 软件包。通过 Nuget 软件包管理器安装 ZeroTouchLibrary 软件包。对于 using Autodesk.DesignScript.Geometry; 语句而言,此软件包是必需的。

  1. 浏览 ZeroTouchLibrary 软件包

  2. 我们将要在 Dynamo Studio 的当前版本(即 1.3)中使用此节点。选择与此版本匹配的软件包版本。

  3. 请注意,我们还将类文件重命名为 Grids.cs

接下来,我们需要建立 RectangularGrid 方法将在其中运行的名称空间和类。在 Dynamo 中,该节点将根据方法和类名进行命名。我们尚不需要将该节点复制到 Visual Studio 中。

using Autodesk.DesignScript.Geometry;
using System.Collections.Generic;

namespace CustomNodes
{
    public class Grids
    {
        public static List<Rectangle> RectangularGrid(int xCount, int yCount)
        {
        //The method for creating a rectangular grid will live in here
        }
    }
}

Autodesk.DesignScript.Geometry; 将参照创建列表时所需的 ZeroTouchLibrary 软件包 System.Collections.Generic 中的 ProtoGeometry.dll

现在,我们可以添加用于绘制矩形的方法。类文件应如下所示,并可以复制到 Visual Studio 中。

using Autodesk.DesignScript.Geometry;
using System.Collections.Generic;

namespace CustomNodes
{
    public class Grids
    {
        public static List<Rectangle> RectangularGrid(int xCount, int yCount)
        {
            double x = 0;
            double y = 0;

            var pList = new List<Rectangle>();

            for (int i = 0; i < xCount; i++)
            {
                y++;
                x = 0;
                for (int j = 0; j < yCount; j++)
                {
                    x++;
                    Point pt = Point.ByCoordinates(x, y);
                    Vector vec = Vector.ZAxis();
                    Plane bP = Plane.ByOriginNormal(pt, vec);
                    Rectangle rect = Rectangle.ByWidthLength(bP, 1, 1);
                    pList.Add(rect);
                }
            }
            return pList;
        }
    }
}

如果项目看起来与此类似,请继续并尝试构建 .dll。

  1. 选择“构建”>“构建解决方案”

检查项目的 bin 文件夹以查找 .dll。在构建成功后,我们可以将 .dll 添加到 Dynamo 中。

  1. Dynamo 库中的自定义 RectangularGrids 节点

  2. 画布上的自定义节点

  3. 用于将 .dll 添加到 Dynamo 中的“添加”按钮

自定义节点修改

在上例中,我们创建了一个非常简单的节点,该节点未定义 RectangularGrids 方法之外的太多其他内容。但是,我们可能要为输入端口创建工具提示,或者像标准 Dynamo 节点一样为该节点提供摘要。将这些功能添加到自定义节点会使其更易于使用,尤其是当用户要在库中搜索这些节点时。

  1. 默认输入值

  2. xCount 输入的工具提示

RectangularGrid 节点需要其中的一些基本功能。在下面的代码中,我们添加了输入和输出端口描述、摘要和默认输入值。

using Autodesk.DesignScript.Geometry;
using System.Collections.Generic;

namespace CustomNodes
{
    public class Grids
    {
        /// <summary>
        /// This method creates a rectangular grid from an X and Y count.
        /// </summary>
        /// <param name="xCount">Number of grid cells in the X direction</param>
        /// <param name="yCount">Number of grid cells in the Y direction</param>
        /// <returns>A list of rectangles</returns>
        /// <search>grid, rectangle</search>
        public static List<Rectangle> RectangularGrid(int xCount = 10, int yCount = 10)
        {
            double x = 0;
            double y = 0;

            var pList = new List<Rectangle>();

            for (int i = 0; i < xCount; i++)
            {
                y++;
                x = 0;
                for (int j = 0; j < yCount; j++)
                {
                    x++;
                    Point pt = Point.ByCoordinates(x, y);
                    Vector vec = Vector.ZAxis();
                    Plane bP = Plane.ByOriginNormal(pt, vec);
                    Rectangle rect = Rectangle.ByWidthLength(bP, 1, 1);
                    pList.Add(rect);
                    Point cPt = rect.Center();
                }
            }
            return pList;
        }
    }
}
  • 通过为方法参数赋值,来为输入提供默认值:RectangularGrid(int xCount = 10, int yCount = 10)

  • 创建输入和输出工具提示、搜索关键字以及前面带有 /// 的 XML 文档摘要。

要添加工具提示,我们需要在项目目录中有一个 xml 文件。通过启用该选项,.xml 可以由 Visual Studio 自动生成。

  1. 在此处启用 XML 文档文件,然后指定文件路径。这将生成一个 XML 文件。

大功告成!我们已创建了一个包含多个标准功能的新节点。以下“Zero-Touch 基础知识”章节将更详细地介绍 Zero-Touch 节点开发以及需要注意的问题。

矩形网格图形
在 Visual Studio 中创建新项目
在 Visual Studio 中配置新项目
ZeroTouchLibrary 软件包
构建 DLL
Dynamo 中的自定义节点
输入工具提示
启用 XML 文档