Skip to content

Instantly share code, notes, and snippets.

@saitohiroaki1122
Last active May 14, 2020 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saitohiroaki1122/eda1c17ebefbb08e00bb2310979b97cd to your computer and use it in GitHub Desktop.
Save saitohiroaki1122/eda1c17ebefbb08e00bb2310979b97cd to your computer and use it in GitHub Desktop.
GH C# Component tips_寸法線を記述します(Rhino6版)
using System;
using System.Collections;
using System.Collections.Generic;
using Rhino;
using Rhino.Geometry;
using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
using System.Drawing;
using Rhino.DocObjects;
/// <summary>
/// This class will be instantiated on demand by the Script component.
/// </summary>
public class Script_Instance : GH_ScriptInstance
{
#region Utility functions
/// <summary>Print a String to the [Out] Parameter of the Script component.</summary>
/// <param name="text">String to print.</param>
private void Print(string text) { /* Implementation hidden. */ }
/// <summary>Print a formatted String to the [Out] Parameter of the Script component.</summary>
/// <param name="format">String format.</param>
/// <param name="args">Formatting parameters.</param>
private void Print(string format, params object[] args) { /* Implementation hidden. */ }
/// <summary>Print useful information about an object instance to the [Out] Parameter of the Script component. </summary>
/// <param name="obj">Object instance to parse.</param>
private void Reflect(object obj) { /* Implementation hidden. */ }
/// <summary>Print the signatures of all the overloads of a specific method to the [Out] Parameter of the Script component. </summary>
/// <param name="obj">Object instance to parse.</param>
private void Reflect(object obj, string method_name) { /* Implementation hidden. */ }
#endregion
#region Members
/// <summary>Gets the current Rhino document.</summary>
private readonly RhinoDoc RhinoDocument;
/// <summary>Gets the Grasshopper document that owns this script.</summary>
private readonly GH_Document GrasshopperDocument;
/// <summary>Gets the Grasshopper script component that owns this script.</summary>
private readonly IGH_Component Component;
/// <summary>
/// Gets the current iteration count. The first call to RunScript() is associated with Iteration==0.
/// Any subsequent call within the same solution will increment the Iteration count.
/// </summary>
private readonly int Iteration;
#endregion
/// <summary>
/// This procedure contains the user code. Input parameters are provided as regular arguments,
/// Output parameters as ref arguments. You don't have to assign output parameters,
/// they will have a default value.
/// </summary>
private void RunScript(bool bake)
{
//Planeを適当に作成。
Point3d plnPt1 = new Point3d(200, 400, 0);
Point3d plnPt2 = new Point3d(900, 600, 0);
Point3d plnPt3 = new Point3d(1200, 800, 0);
Plane dimPlane = new Plane(plnPt1, plnPt2, plnPt3);
//寸法の間隔を点でリストに追加。Point2dであることに注意。
Point2d dimPt1 = new Point2d(0, -150);
Point2d dimPt2 = new Point2d(1500, -150);
//LinearDimensionオブジェクトを作成。
LinearDimension lineDim =
new LinearDimension(dimPlane, dimPt1, dimPt2, new Point2d(dimPt1.X, dimPt1.Y - 250));
//寸法スタイルを設定する。
lineDim.ParentDimensionStyle = this.FindDimStyleByName("dim_GH-Bake");
//描画のためにひとつのデータでもリストに入れてから描画した方が無難。計算の回し方で問題を起こす場合があるので。
this.lineDimList.Add(lineDim);
//ボタンを押したらBakeする。
if(bake)
{
//doc.Objects.AddLinearDimension(lineDim); でもOK。
Rhino.RhinoDoc.ActiveDoc.Objects.AddLinearDimension(lineDim);
}
}
// <Custom additional code>
//RunScriptの{}の外にデータを渡すために外で宣言する。
public List<LinearDimension> lineDimList = new List<LinearDimension>();
public DimensionStyle FindDimStyleByName(string dimStyleName)
{
DimensionStyle dimStyle = doc.DimStyles.FindName(dimStyleName);
if(dimStyle == null)
{
//指定した名前のDimensionStyleがなかった場合は新規作成する。
DimensionStyle newDimStyle = new DimensionStyle();
//DimensionStyleのプロパティについては下記説明とほぼ同じ単語なので参照
//https://docs.mcneel.com/rhino/6/help/es-es/documentproperties/dimensions_style.htm
newDimStyle.LengthResolution = 1;
newDimStyle.AngleResolution = 2;
newDimStyle.TextHeight = 80;
newDimStyle.TextGap = 20;
newDimStyle.ExtensionLineExtension = 0;
newDimStyle.ExtensionLineOffset = 10;
newDimStyle.CentermarkSize = 30;
newDimStyle.ArrowType1 = DimensionStyle.ArrowType.Dot;
newDimStyle.ArrowType2 = DimensionStyle.ArrowType.Dot;
newDimStyle.ArrowLength = 40;
newDimStyle.LeaderArrowType = DimensionStyle.ArrowType.Dot;
newDimStyle.LeaderArrowLength = 40;
newDimStyle.TextVerticalAlignment = TextVerticalAlignment.Bottom;
newDimStyle.TextHorizontalAlignment = TextHorizontalAlignment.Center;
newDimStyle.Name = dimStyleName;
newDimStyle.Index = doc.DimStyles.Add(newDimStyle, false);
return newDimStyle;
}
else
{
//DimensionStyleに名前があった場合はそのStyleを返す。
return dimStyle;
}
}
public override void DrawViewportWires(IGH_PreviewArgs args)
{
foreach(LinearDimension item in lineDimList)
{
//Rhino6からDrawAnnotationで寸法が描画できる。
args.Display.DrawAnnotation(item, Color.Red);
}
}
public override BoundingBox ClippingBox
{
get
{
//GHファイル内に、このコンポーネント以外にオブジェクトがない場合はこのBoundingBoxを作っておく必要がある。
return lineDimList[0].GetBoundingBox(Plane.WorldXY);;
}
}
public override void BeforeRunScript()
{
//RunScriptの{}の外にリストを宣言、newした場合はこのメソッドの中でClear()しておく。
lineDimList.Clear();
}
// </Custom additional code>
}
@saitohiroaki1122
Copy link
Author

captureDrawAndBakeLinearDimension

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment