• openDDLについて
  • サイトポリシー
  • ご利用前にお読みください
  • EN
  • SIGN IN:
  • SIGN IN:
GHC#_Text3dとTextEntityについて、描画とBakeした際のふるまいの比較
by Hiroaki Saito May 16, 2020

captureText3dTextEntityComparison.PNG

Text Entityについて別の記事で補足をしています。(2020/5/23追記)

表題の内容を確認してみました。コードは下の方にあります。ポイントとしては、

Text3dは、
・簡易で良いが改行したテキストを扱う際、位置合わせ次第でDrawViewportWiresを使った描画にバグがある。
・GH上の描画がおかしいだけで正常にBakeはできるけど、Rhino側の既定の寸法スタイルでBakeされるので注意が必要。

TextEntityは、
・最初に寸法スタイルを設定する必要があるので、Text3dに比べて手間がかかる。
・位置合わせはRhino側にある寸法スタイルに依存しているので、TextEntityの位置合わせを使ってもGH上の描画は変わらない。
・ただし、TextEntityの位置合わせはBakeの際にだけ寸法スタイルに上書かれて、設定通りにBakeされる。

簡易なものであればText3dで、細やかに管理したい場合はTextEntityでやるのが良いのかなと思いました。
因みにRhino5時代であればText3dには位置合わせの機能がなくて、TextEntityには描画機能が用意されていなかったので、色々工夫する必要がありました。

Draw3dTextメソッドにはstringを直接描画するのも用意されていますが、おおよそText3dと同じなので割愛しています。
より簡易に、軽く描画したい場合にはDraw2dTextを使う方が良いかもしれません。
GHのText TagコンポーネントとText Tag 3Dコンポーネントの違い的なもので、使い分けになる思います。

Text3dAndTextEntityComparison

コード全部は上のリンクに載せていますが、RunScript以下のコードをここにも載せておきます。追加のusingは以下の3つです。

using Rhino.Display;
using Rhino.DocObjects;
using System.Drawing;

private void RunScript(bool bake, ref object A)
{
//Text描画の基準面をそれぞれ作っておく。
Plane txt3dPln = new Plane(new Point3d(0, 300, 0), Vector3d.ZAxis);
Plane txtEntityPln = new Plane(new Point3d(600, 300, 0), Vector3d.ZAxis);

//出力して表示するためのList。
List

<Plane> plnList = new List<Plane>();
plnList.Add(txt3dPln);
plnList.Add(txtEntityPln);

//Text3dの場合はnewしてオブジェクトを作る。寸法スタイルを設定しなくていいので楽ではある。
txt3d = new Text3d("TextTest\nText3d", txt3dPln, 80);

//Text3dはDimensionStyleで定義されないので、位置合わせが扱い易い。
//しかし、改行したテキストの場合、位置合わせ次第ではGH上の描画がうまくいかないバグがあるので注意が必要。
txt3d.HorizontalAlignment = TextHorizontalAlignment.Center;
txt3d.VerticalAlignment = TextVerticalAlignment.Bottom;

//TextEntityの場合は寸法スタイルを設定する必要がある。
DimensionStyle dimStyle = this.FindDimStyleByName("dim_GH-Bake");

//TextEntityはStaticメソッドを呼び出してオブジェクトを作る。
txtEntity = TextEntity.Create(
"TextTest\nTextEntity", txtEntityPln, dimStyle, false, 0, 0);

//TextEntityの位置合わせをしてもGH上の描画には反映されない。そうしたい場合は、寸法スタイルを変更する必要がある。
//Bakeした時には、この設定が寸法スタイルに上書かれて反映される。
txtEntity.Justification = Rhino.Geometry.TextJustification.BottomCenter;

//Bakeするやつ。
if(bake)
{
RhinoDoc.ActiveDoc.Objects.AddText(txt3d);
RhinoDoc.ActiveDoc.Objects.AddText(txtEntity);
}

A = plnList;
}

//

<Custom additional code>
//Text3dはRhino.Display Namespace、TextEntityはRhino.Geometry Namespaceと違いがあります。
//理解のために、あえて頭から書いているけど、usingで書いていれば省略可能です。
Rhino.Display.Text3d txt3d;
Rhino.Geometry.TextEntity txtEntity;

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

//Draw all wires and points in this method.
public override void DrawViewportWires(IGH_PreviewArgs args)
{
//Text3dを描画。改行したテキストを扱うと位置合わせ次第で正常に表示されないバグがある。
args.Display.Draw3dText(txt3d, Color.Red);

//TextEntityを描画。
args.Display.DrawText(txtEntity, 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.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;
}
}
//

</Custom additional code>


コメントをするにはログインしてください。

5847 0
Tags
#C# #DrawViewportWires #Grasshopper #bake #TextEntity #Text3d
License
cc_by_sa
GNU GPL

CONTACT ©2025 NIKKEN SEKKEI LTD.