Skip to content

Instantly share code, notes, and snippets.

@saitohiroaki1122
Created May 23, 2020 05:45
Show Gist options
  • Save saitohiroaki1122/c1433b36907a53583324e3d35d7f88dc to your computer and use it in GitHub Desktop.
Save saitohiroaki1122/c1433b36907a53583324e3d35d7f88dc to your computer and use it in GitHub Desktop.
GH C#_TextEntityオブジェクトの作成について、コンストラクタと静的メソッドでの違い
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 Rhino.Display;
using Rhino.DocObjects;
using System.Drawing;
/// <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, ref object A)
{
//Text描画の基準面をそれぞれ作っておく。
Plane planeTextWithSM = new Plane(new Point3d(0, 300, 0), Vector3d.ZAxis);
Plane planeTextWithC = new Plane(new Point3d(900, 300, 0), Vector3d.ZAxis);
//出力して表示するためのList。
List<Plane> plnList = new List<Plane>();
plnList.Add(planeTextWithSM);
plnList.Add(planeTextWithC);
//TextEntityの場合は寸法スタイルを設定する必要がある。
DimensionStyle dimStyle = this.FindDimStyleByName("dim_GH-Bake");
//静的メソッドによる作成。
txtEntityWithSM =
TextEntity.Create("TextWithSM", planeTextWithSM, dimStyle, false, 0, 0);
//GH上の描画には反映されず、Bake時に反映される。
txtEntityWithSM.TextHeight = 40;
txtEntityWithSM.Justification = TextJustification.BottomCenter;
//コンストラクタによる作成。
txtEntityWithC = new TextEntity();
txtEntityWithC.Plane = planeTextWithC;
txtEntityWithC.ParentDimensionStyle = dimStyle;
//TextをSetする前に各種設定をSetする。
txtEntityWithC.TextHeight = 40;
txtEntityWithC.Justification = TextJustification.BottomCenter;
//一通り設定してから最後にTextをSetする。
txtEntityWithC.PlainText = "TextWithC";
//TextをSetした後にサイズや位置合わせをしてもGH上の描画には反映されない。(Bake時には反映される。)
//txtEntityWithC.TextHeight = 40;
//txtEntityWithC.Justification = TextJustification.BottomCenter;
//Bakeするやつ。
if(bake)
{
RhinoDoc.ActiveDoc.Objects.AddText(txtEntityWithSM);
RhinoDoc.ActiveDoc.Objects.AddText(txtEntityWithC);
}
A = plnList;
}
// <Custom additional code>
TextEntity txtEntityWithSM;
TextEntity txtEntityWithC;
//Draw all wires and points in this method.
public override void DrawViewportWires(IGH_PreviewArgs args)
{
args.Display.DrawText(txtEntityWithSM, Color.Red);
args.Display.DrawText(txtEntityWithC, Color.Blue);
}
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.TextHeight = 80;
newDimStyle.Name = dimStyleName;
newDimStyle.Index = doc.DimStyles.Add(newDimStyle, false);
return newDimStyle;
}
else
{
//DimensionStyleに名前があった場合はそのStyleを返す。
return dimStyle;
}
}
// </Custom additional code>
}
@saitohiroaki1122
Copy link
Author

captureTextEntityComparison

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