Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GH C# Component tips_寸法線を記述します (Rhino5版)
private void RunScript(bool bake, string name, ref object A)
{
//Planeを適当に作成
Point3d pt1 = new Point3d(200, 400, 0);
Point3d pt2 = new Point3d(900, 600, 0);
Point3d pt3 = new Point3d(1200, 800, 0);
Plane plane = new Plane(pt1, pt2 - pt1, pt3 - pt1);
//寸法の間隔を点でリストに追加
List<Point2d> pts = new List<Point2d>();
pts.Add(new Point2d(0, -150));
pts.Add(new Point2d(1500, -150));
pts.Add(new Point2d(2200.5, -150));
pts.Add(new Point2d(3000, -150));
//寸法の基準PlaneとListを入れると寸法が返ってくるメソッド。DimensionStyleも自動的に作成するためにnameも入れる。
lnDims = GetLinearDimensions(plane, pts, -250, name);
if(bake)
{
foreach(LinearDimension item in lnDims)
{
Rhino.DocObjects.Tables.ObjectTable oTable = Rhino.RhinoDoc.ActiveDoc.Objects;
oTable.AddLinearDimension(item);
}
}
//参考にPlaneの位置を出力で表示
A = plane;
}
// <Custom additional code>
public List<LinearDimension> lnDims;
public Color dimColor = Color.Green;
//寸法を作成するメソッド
public List<LinearDimension> GetLinearDimensions(
Plane dimPlane, List<Point2d> extendLnEndPts, double offsetDimLn, string dimStyleName)
{
List<LinearDimension> lnDims = new List<LinearDimension>();
LinearDimension lnDim;
for(int i = 0; i < extendLnEndPts.Count - 1; i++)
{
lnDim = new LinearDimension(dimPlane, extendLnEndPts[i], extendLnEndPts[i + 1],
new Point2d(extendLnEndPts[i].X, extendLnEndPts[i].Y + offsetDimLn));
lnDim.DimensionStyleIndex = FindStyleIndexByName(dimStyleName);
//距離がゼロの場合は寸法をリストに追加しない
if(Math.Round(lnDim.DistanceBetweenArrowTips) != 0)
{
lnDims.Add(lnDim);
}
}
return lnDims;
}
//参照元:http://www.grasshopper3d.com/forum/topics/how-to-add-dimensionstyle-to-the-lineardimension
public int FindStyleIndexByName(string dimStyleName)
{
DimensionStyle dimStyle = doc.DimStyles.Find(dimStyleName, true);
if(dimStyle == null) //DimensionStyleに名前がなかった場合はStyleを新規作成してIndexを返す
{
int dimStyleIndex = doc.DimStyles.Add(dimStyleName, false);
DimensionStyle newDimStyle = doc.DimStyles[dimStyleIndex];
//DimensionStyleのプロパティについては下記説明とほぼ同じ単語なので参照
//https://docs.mcneel.com/rhino/5/help/en-us/documentproperties/dimensions_styles.htm
newDimStyle.LengthResolution = 2;
newDimStyle.AngleResolution = 2;
newDimStyle.TextHeight = 100;
newDimStyle.TextGap = 20;
newDimStyle.ExtensionLineExtension = 0;
newDimStyle.ExtensionLineOffset = 10;
newDimStyle.CenterMarkSize = 30;
newDimStyle.ArrowType = DimensionStyleArrowType.Dot;
newDimStyle.ArrowLength = 40;
newDimStyle.LeaderArrowType = DimensionStyleArrowType.Dot;
newDimStyle.LeaderArrowLength = 40;
newDimStyle.TextAlignment = TextDisplayAlignment.AboveLine;
newDimStyle.CommitChanges();
return dimStyleIndex;
}
else //DimensionStyleに名前があった場合はそのIndexを返す
{
return dimStyle.Index;
}
}
//C#コンポーネント内で線とテキストを記述
//Draw all wires and points in this method.
public override void DrawViewportWires(IGH_PreviewArgs args)
{
/*
DrawViewportWiresメソッドには寸法を直接表示する機能がないため、寸法の情報を利用して疑似寸法を描画する
http://developer.rhino3d.com/api/RhinoCommon/html/T_Rhino_Display_DisplayPipeline.htm
*/
foreach(LinearDimension item in lnDims)
{
Plane dimPln = item.Plane;
Point3d tmpPt1 = dimPln.PointAt(item.Arrowhead1End.X, item.Arrowhead1End.Y);
Point3d tmpPt2 = dimPln.PointAt(item.Arrowhead2End.X, item.Arrowhead2End.Y);
Point3d tmpPt3 = dimPln.PointAt(item.ExtensionLine1End.X, item.ExtensionLine1End.Y);
Point3d tmpPt4 = dimPln.PointAt(item.ExtensionLine2End.X, item.ExtensionLine2End.Y);
Point3d tmpPt5 = dimPln.PointAt(item.TextPosition.X, item.TextPosition.Y);
//args.Displayには様々なDrawメソッドがある
args.Display.DrawLine(new Line(tmpPt1, tmpPt2), dimColor); //寸法線
args.Display.DrawLine(new Line(tmpPt1, tmpPt3), dimColor); //寸法延長線1
args.Display.DrawLine(new Line(tmpPt2, tmpPt4), dimColor); //寸法延長線2
string tmpTxt = Math.Round(new Line(tmpPt1, tmpPt2).Length, 1).ToString();
dimPln = new Plane(tmpPt5, dimPln.XAxis, dimPln.YAxis);
Rhino.Display.Text3d txt3d = new Rhino.Display.Text3d(tmpTxt, dimPln, 100);
txt3d = TextJustification(txt3d, 8);
args.Display.Draw3dText(txt3d, dimColor);
}
}
//テキストの位置合わせを行う。完璧なものではないので参考まで。
private Rhino.Display.Text3d TextJustification(
Rhino.Display.Text3d txt3d, int justificationIndex)
{
/*
1 - Top Left
2 - Top Middle
3 - Top Right
4 - Middle Left
5 - Middle Center
6 - Middle Right
7 - Bottom Left(default)
8 - Bottom Center
9 - Bottom Right
*/
Point3d checkPt = new Point3d(0, 0, 0);
Point3d basePt = new Point3d(0, 0, 0);
double uFromTextPlane, vFromTextPlane;
Rhino.Display.Text3d txt3dJustification = new Rhino.Display.Text3d("temp");
Rectangle3d rect3d = new Rectangle3d(txt3d.TextPlane,
txt3d.TextPlane.Origin, txt3d.BoundingBox.FurthestPoint(txt3d.TextPlane.Origin));
switch(justificationIndex)
{
case 1:
checkPt = rect3d.PointAt(0, 1);
break;
case 2:
checkPt = rect3d.PointAt(0.5, 1);
break;
case 3:
checkPt = rect3d.PointAt(1, 1);
break;
case 4:
checkPt = rect3d.PointAt(0, 0.5);
break;
case 5:
checkPt = rect3d.PointAt(0.5, 0.5);
break;
case 6:
checkPt = rect3d.PointAt(1, 0.5);
break;
case 7:
checkPt = rect3d.PointAt(0, 0);
break;
case 8:
checkPt = rect3d.PointAt(0.5, 0);
break;
case 9:
checkPt = rect3d.PointAt(1, 0);
break;
}
txt3d.TextPlane.ClosestParameter(checkPt, out uFromTextPlane, out vFromTextPlane);
basePt = txt3d.TextPlane.PointAt(-uFromTextPlane, -vFromTextPlane);
txt3d.TextPlane = new Plane(basePt, txt3d.TextPlane.XAxis, txt3d.TextPlane.YAxis);
txt3dJustification = txt3d;
return txt3dJustification;
}
// </Custom additional code>
@saitohiroaki1122

This comment has been minimized.

Copy link
Owner Author

@saitohiroaki1122 saitohiroaki1122 commented Apr 10, 2018

capture_drawdimensions

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