应用程序:桁架(Truss)
Revit平台:结构
Revit版本:2011.0
首次发布:2009.0
编程语言:C#
技能水平:中级
类别:结构,几何
类型:ExternalCommand
主题:创建桁架,编辑桁架剖面和成员。
摘要:
该示例演示如何创建桁架并修改其桁架成员和剖面。
相关类:
Autodesk.Revit.DB.Structure.Truss
Autodesk.Revit.DB.Structure.TrussType
Autodesk.Revit.DB.FamilyInstance
Autodesk.Revit.DB.FamilySymbol
Autodesk.Revit.DB.ViewPlan
Autodesk.Revit.DB.Strucure.TrussMemberInfo
Autodesk.Revit.DB.Line
Autodesk.Revit.DB.CurveArray
项目文件:
Command.cs
该文件包含一个实现IExternalCommand接口的Command类,创建TrussForm并获取ExternalCommandData。
TrussForm.cs
- 该文件包含继承自Form的TrussForm类。该窗体具有一个TabControl,其中包含三个页面:“视图”,“桁架成员”和“剖面编辑”。
- 在“视图”页面中,用户可以在PictureBox中绘制桁架的起始点和终点以在选定的层上创建桁架,所有可用层都应列在ComboBox中。
- 在“桁架成员”页面中,用户创建桁架后,所有桁架的曲线应在图片框中显示,用户可以通过在图片框中单击曲线来选择桁架成员(梁),并更改所选桁架成员的梁类型,所有梁类型都应列在ComboBox中。
- 在“Profile Edit”页面中,用户可以在显示桁架几何信息的图片框中编辑新创建的桁架的剖面,并可以通过单击“Restore”按钮来撤消编辑。此页面还提供了一个“Update”按钮,用于在编辑剖面之后更新桁架的几何图形。
TrussGeometry.cs
该文件包含一个用于计算新创建的桁架的几何信息并更改桁架剖面的TrussGeometry类。 TrussGeometry由Draw2D()和SetProdile()方法组成。Draw2D()用于在图片框上绘制桁架曲线,而SetProfile()用于通过用户输入重置桁架的剖面。
LineTool.cs
该文件包含一个名为LineTool的类,该类提供了一些绘制窗体上的线并存储绘制线数据的方法。
MathTools.cs
该文件包含Verctor4和Matrix4两个类。这两个类用于在3D和2D之间转换点。
描述:
此示例演示了如何通过Autodesk.Revit.Creation.Document.NewTruss(Symbols.TrussType trussType, Elements.SketchPlane sketchPlane, Geometry.Curve baseCurve, Elements.View dbView)方法创建桁架:
- TrussType将指定桁架的类型,可以从Autodesk.Revit.Document.TrussTypes属性获取。
- sketchPlane用于定义桁架可以基于哪个草图平面
- baseCurve用于定义新桁架的起始点和终点。
- dbView用于指定桁架创建的级别。
说明:
1. 在Revit中选择两个柱子或现有的桁架。
2. 运行该命令。(如果项目中没有桁架类型或柱子模板,则应首先加载一些,或者打开Truss.rvt)
3. 如果用户选择了两个柱子,则应在ComboBox中选择要创建桁架的级别,然后单击“Create”按钮以创建桁架。
4. 创建新桁架后,可以打开“Truss Members”和“Profile Edit”选项卡。
5. 打开“桁架成员”页面,您可以通过单击桁架几何中的每条曲线来选择桁架成员。当它变为红色时,您可以通过“Change Type”按钮更改其梁类型,您可以在ComboBox中选择要更改的梁类型。
6. 打开“Profile Edit”页面,用户可以在图片框中绘制TopChord线和BottomChord线,完成对桁架的新剖面后,单击“Update”按钮,您可以通过“Restore”按钮还原桁架剖面。
源代码:
完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码
LineTool.cs
MathTools.cs
//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.Revit.DB;
namespace Revit.SDK.Samples.Truss.CS
{
/// <summary>
/// Vector4 is a homogeneous coordinate class used to store vector
/// and contain method to handle the vector
/// </summary>
public class Vector4
{
#region Class member variables and properties
private double m_x;
private double m_y;
private double m_z;
private double m_w = 1.0f;
/// <summary>
/// X property to get/set x value of Vector4
/// </summary>
public double X
{
get
{
return m_x;
}
set
{
m_x = value;
}
}
/// <summary>
/// Y property to get/set y value of Vector4
/// </summary>
public double Y
{
get
{
return m_y;
}
set
{
m_y = value;
}
}
/// <summary>
/// Z property to get/set z value of Vector4
/// </summary>
public double Z
{
get
{
return m_z;
}
set
{
m_z = value;
}
}
/// <summary>
/// W property to get/set fourth value of Vector4
/// </summary>
public double W
{
get
{
return m_w;
}
set
{
m_w = value;
}
}
#endregion
/// <summary>
/// constructor
/// </summary>
public Vector4(double x, double y, double z)
{
this.X = x; this.Y = y; this.Z = z;
}
/// <summary>
/// constructor, transfer Autodesk.Revit.DB.XYZ to vector
/// </summary>
/// <param name="v">Autodesk.Revit.DB.XYZ structure which needs to be transferred</param>
public Vector4(Autodesk.Revit.DB.XYZ v)
{
this.X = (double)v.X; this.Y = (double)v.Y; this.Z = (double)v.Z;
}
/// <summary>
/// adds two vectors
/// </summary>
/// <param name="va">first vector</param>
/// <param name="vb">second vector</param>
public static Vector4 operator+ (Vector4 va, Vector4 vb)
{
return new Vector4(va.X + vb.X, va.Y + vb.Y, va.Z + vb.Z);
}
/// <summary>
/// subtracts two vectors
/// </summary>
/// <param name="va">first vector</param>
/// <param name="vb">second vector</param>
/// <returns>subtraction of two vectors</returns>
public static Vector4 operator- (Vector4 va, Vector4 vb)
{
return new Vector4(va.X - vb.X, va.Y - vb.Y, va.Z - vb.Z);
}
/// <summary>
/// multiplies a vector by a doubling type value
/// </summary>
/// <param name="v">vector</param>
/// <param name="factor">multiplier of doubling type</param>
/// <returns> the result vector </returns>
public static Vector4 operator* (Vector4 v,double factor)
{
return new Vector4(v.X * factor, v.Y * factor, v.Z * factor);
}
/// <summary>
/// divides vector by a double type value
/// </summary>
/// <param name="v">vector</param>
/// <param name="factor">doubling type value</param>
/// <returns> vector divided by a doubling type value </returns>
public static Vector4 operator /(Vector4 v, double factor)
{
return new Vector4(v.X / factor, v.Y / factor, v.Z / factor);
}
/// <summary>
/// dot multiply vector
/// </summary>
/// <param name="v"> the result vector </param>
public double DotProduct(Vector4 v)
{
return (this.X * v.X + this.Y * v.Y + this.Z * v.Z);
}
/// <summary>
/// get normal vector of plane contains two vectors
/// </summary>
/// <param name="v">second vector</param>
/// <returns> normal vector of two vectors</returns>
public Vector4 CrossProduct(Vector4 v)
{
return new Vector4(this.Y * v.Z - this.Z * v.Y,this.Z * v.X
- this.X * v.Z,this.X * v.Y - this.Y * v.X);
}
/// <summary>
/// dot multiply two vectors
/// </summary>
/// <param name="va">first vector</param>
/// <param name="vb">second vector</param>
public static double DotProduct(Vector4 va, Vector4 vb)
{
return (va.X * vb.X + va.Y * vb.Y + va.Z * vb.Z);
}
/// <summary>
/// get normal vector of two vectors
/// </summary>
/// <param name="va">first vector</param>
/// <param name="vb">second vector</param>
/// <returns> normal vector of two vectors </returns>
public static Vector4 CrossProduct(Vector4 va, Vector4 vb)
{
return new Vector4(va.Y * vb.Z - va.Z * vb.Y, va.Z * vb.X
- va.X * vb.Z, va.X * vb.Y - va.Y * vb.X);
}
/// <summary>
/// get unit vector
/// </summary>
public void Normalize()
{
double length = Length();
if(length == 0)
{
length = 1;
}
this.X /= length;
this.Y /= length;
this.Z /= length;
}
/// <summary>
/// calculate the length of vector
/// </summary>
public double Length()
{
return (double)Math.Sqrt(this.X * this.X + this.Y * this.Y + this.Z * this.Z);
}
};
/// <summary>
/// Matrix used to transform between ucs coordinate and world coordinate.
/// </summary>
public class Matrix4
{
#region MatrixType
/// <summary>
/// Matrix Type Enum use to define function of matrix
/// </summary>
public enum MatrixType
{
/// <summary>
/// matrix use to rotate
/// </summary>
Rotation,
/// <summary>
/// matrix used to Translation
/// </summary>
Translation,
/// <summary>
/// matrix used to Scale
/// </summary>
Scale,
/// <summary>
/// matrix used to Rotation and Translation
/// </summary>
RotationAndTranslation,
/// <summary>
/// normal matrix
/// </summary>
Normal
};
private double[,] m_matrix = new double[4,4]; // an array stores the matrix
private MatrixType m_type; //type of matrix
#endregion
/// <summary>
/// default constructor
/// </summary>
public Matrix4()
{
m_type = MatrixType.Normal;
Identity();
}
/// <summary>
/// ctor,rotation matrix,origin at (0,0,0)
/// </summary>
/// <param name="xAxis">identity of x axis</param>
/// <param name="yAxis">identity of y axis</param>
/// <param name="zAxis">identity of z axis</param>
public Matrix4(Vector4 xAxis,Vector4 yAxis, Vector4 zAxis)
{
m_type = MatrixType.Rotation;
Identity();
m_matrix[0, 0] = xAxis.X; m_matrix[0, 1] = xAxis.Y; m_matrix[0, 2] = xAxis.Z;
m_matrix[1, 0] = yAxis.X; m_matrix[1, 1] = yAxis.Y; m_matrix[1, 2] = yAxis.Z;
m_matrix[2, 0] = zAxis.X; m_matrix[2, 1] = zAxis.Y; m_matrix[2, 2] = zAxis.Z;
}
/// <summary>
/// ctor,translation matrix.
/// </summary>
/// <param name="origin">origin of ucs in world coordinate</param>
public Matrix4(Vector4 origin)
{
m_type = MatrixType.Translation;
Identity();
m_matrix[3, 0] = origin.X; m_matrix[3, 1] = origin.Y; m_matrix[3, 2] = origin.Z;
}
/// <summary>
/// rotation and translation matrix constructor
/// </summary>
/// <param name="xAxis">x Axis</param>
/// <param name="yAxis">y Axis</param>
/// <param name="zAxis">z Axis</param>
/// <param name="origin">origin</param>
public Matrix4(Vector4 xAxis, Vector4 yAxis, Vector4 zAxis, Vector4 origin)
{
m_type = MatrixType.RotationAndTranslation;
Identity();
m_matrix[0, 0] = xAxis.X; m_matrix[0, 1] = xAxis.Y; m_matrix[0, 2] = xAxis.Z;
m_matrix[1, 0] = yAxis.X; m_matrix[1, 1] = yAxis.Y; m_matrix[1, 2] = yAxis.Z;
m_matrix[2, 0] = zAxis.X; m_matrix[2, 1] = zAxis.Y; m_matrix[2, 2] = zAxis.Z;
m_matrix[3, 0] = origin.X; m_matrix[3, 1] = origin.Y; m_matrix[3, 2] = origin.Z;
}
/// <summary>
/// scale matrix constructor
/// </summary>
/// <param name="scale">scale factor</param>
public Matrix4(double scale)
{
m_type = MatrixType.Scale;
Identity();
m_matrix[0, 0] = scale;
m_matrix[1, 1] = scale;
m_matrix[2, 2] = scale;
}
/// <summary>
/// indexer of matrix
/// </summary>
/// <param name="row">row number</param>
/// <param name="column">column number</param>
/// <returns></returns>
public double this[int row, int column]
{
get
{
return this.m_matrix[row, column];
}
set
{
this.m_matrix[row, column] = value;
}
}
/// <summary>
/// Identity matrix
/// </summary>
public void Identity()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
this.m_matrix[i, j] = 0.0f;
}
}
this.m_matrix[0, 0] = 1.0f;
this.m_matrix[1, 1] = 1.0f;
this.m_matrix[2, 2] = 1.0f;
this.m_matrix[3, 3] = 1.0f;
}
/// <summary>
/// multiply matrix left and right
/// </summary>
/// <param name="left">left matrix</param>
/// <param name="right">right matrix</param>
/// <returns></returns>
public static Matrix4 Multiply(Matrix4 left, Matrix4 right)
{
Matrix4 result = new Matrix4();
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
result[i, j] = left[i, 0] * right[0, j] + left[i, 1] * right[1, j]
+ left[i, 2] * right[2, j] + left[i, 3] * right[3, j];
}
}
return result;
}
/// <summary>
/// transform point using this matrix
/// </summary>
/// <param name="point">point to be transformed</param>
/// <returns>transform result</returns>
public Vector4 Transform(Vector4 point)
{
return new Vector4(point.X * this[0, 0] + point.Y * this[1, 0]
+ point.Z * this[2, 0]+ point.W * this[3, 0],
point.X * this[0, 1] + point.Y * this[1, 1]
+ point.Z * this[2, 1]+ point.W * this[3, 1],
point.X * this[0, 2] + point.Y * this[1, 2]
+ point.Z * this[2, 2]+ point.W * this[3, 2]);
}
/// <summary>
/// if m_matrix is a rotation matrix,this method can get the rotation inverse matrix.
/// </summary>
/// <returns>inverse of rotation matrix</returns>
public Matrix4 RotationInverse()
{
return new Matrix4(new Vector4(this[0, 0], this[1, 0], this[2, 0]),
new Vector4(this[0, 1], this[1, 1], this[2, 1]),
new Vector4(this[0, 2], this[1, 2], this[2, 2]));
}
/// <summary>
/// if this m_matrix is a translation matrix,
/// this method can get the translation inverse matrix.
/// </summary>
/// <returns>inverse of translation matrix</returns>
public Matrix4 TranslationInverse()
{
return new Matrix4(new Vector4(-this[3, 0], -this[3, 1], -this[3, 2]));
}
/// <summary>
/// get inverse matrix
/// </summary>
/// <returns>inverse matrix</returns>
public Matrix4 Inverse()
{
switch(m_type)
{
case MatrixType.Rotation:
return RotationInverse();
case MatrixType.Translation:
return TranslationInverse();
case MatrixType.RotationAndTranslation:
return Multiply(TranslationInverse(),RotationInverse());
case MatrixType.Scale:
return ScaleInverse();
case MatrixType.Normal:
return new Matrix4();
default: return null;
}
}
/// <summary>
/// if m_matrix is a scale matrix,this method can get the scale inverse matrix.
/// </summary>
/// <returns>inverse of scale matrix</returns>
public Matrix4 ScaleInverse()
{
return new Matrix4(1 / m_matrix[0,0]);
}
};
}
TrussGeometry.cs
//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Autodesk.Revit;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Collections;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB.Structure;
using Point = System.Drawing.Point;
namespace Revit.SDK.Samples.Truss.CS
{
/// <summary>
/// TrussGeometry class contains Geometry information of new created Truss,
/// and contains methods used to Edit profile of truss.
/// </summary>
class TrussGeometry
{
#region class member variables
Autodesk.Revit.DB.Structure.Truss m_truss; //object of truss in Revit
LineTool m_topChord; //line tool used to draw top chord
LineTool m_bottomChord; //line tool used to draw top chord
ArrayList m_graphicsPaths; //store all the GraphicsPath objects of each curve in truss.
int m_selectMemberIndex = -1; // index of selected truss member (beam), -1 when nothing selected.
int m_clickMemberIndex = -1; // index of clicked truss member (beam), -1 when nothing clicked.
List<XYZ> m_points; // store all the points on the needed face
Autodesk.Revit.DB.XYZ[] m_boundPoints; // store array store bound point of truss
Matrix4 m_to2DMatrix = null; // store the Matrix used to transform 3D points to 2D
Matrix4 m_moveToCenterMatrix = null; // store the Matrix used to move points to center
Matrix4 m_scaleMatrix = null; // store the Matrix used to scale profile fit to pictureBox
Matrix4 m_transformMatrix = null; // store the Matrix used to transform Revit coordinate to window UI
Matrix4 m_restoreMatrix = null; // store the Matrix used to transform window UI coordinate to Revit
Matrix4 m_2DToTrussProfileMatrix = null; //store matrix use to transform point on pictureBox to truss (profile) plane
Vector4 m_origin = null; //base point of truss
ExternalCommandData m_commandData; //object which contains reference of Revit Application
Autodesk.Revit.DB.XYZ startLocation = null; //store the start point of truss location
Autodesk.Revit.DB.XYZ endLocation = null; //store the end point of truss location
#endregion
/// <summary>
/// constructor
/// </summary>
/// <param name="truss">new created truss object in Revit</param>
public TrussGeometry(Autodesk.Revit.DB.Structure.Truss truss, ExternalCommandData commandData)
{
m_commandData = commandData;
m_topChord = new LineTool();
m_bottomChord = new LineTool();
m_truss = truss;
m_graphicsPaths = new ArrayList();
GetTrussGeometryInfo();
}
/// <summary>
/// Calculate geometry info for truss
/// </summary>
private void GetTrussGeometryInfo()
{
// get the start and end point of the basic line of the truss
m_points = GetTrussPoints();
// Get a matrix which can transform points to 2D
m_to2DMatrix = GetTo2DMatrix();
// get the boundary of all the points
m_boundPoints = GetBoundsPoints();
// get a matrix which can keep all the points in the center of the canvas
m_moveToCenterMatrix = GetMoveToCenterMatrix();
// get a matrix for scaling all the points and lines within the canvas
m_scaleMatrix = GetScaleMatrix();
// transform 3D points to 2D
m_transformMatrix = Get3DTo2DMatrix();
// transform from 2D to 3D
m_restoreMatrix = Get2DTo3DMatrix();
// transform from 2D (on picture box) to truss profile plane
m_2DToTrussProfileMatrix = Get2DToTrussProfileMatrix();
// create the graphics path which contains all the lines
CreateGraphicsPath();
}
/// <summary>
/// Get points of the truss
/// </summary>
/// <returns>points array stores all the points on truss</returns>
public List<XYZ> GetTrussPoints()
{
List<XYZ> xyzArray = new List<XYZ>();
try
{
IEnumerator iter = m_truss.Members.GetEnumerator();
iter.Reset();
while (iter.MoveNext())
{
Autodesk.Revit.DB.ElementId id = (Autodesk.Revit.DB.ElementId)(iter.Current);
Autodesk.Revit.DB.Element elem =
m_commandData.Application.ActiveUIDocument.Document.GetElement(id);
FamilyInstance familyInstace = (FamilyInstance)(elem);
AnalyticalModel frame = familyInstace.GetAnalyticalModel();
Line line = (Line)(frame.GetCurve());
xyzArray.Add(line.GetEndPoint(0));
xyzArray.Add(line.GetEndPoint(1));
}
}
catch (System.ArgumentException)
{
TaskDialog.Show("Revit", "The start point and the end point of the line are too close, please re-draw it.");
}
return xyzArray;
}
/// <summary>
/// Get a matrix which can transform points to 2D
/// </summary>
/// <returns>matrix which can transform points to 2D</returns>
public Matrix4 GetTo2DMatrix()
{
Line trussLocation = (m_truss.Location as LocationCurve).Curve as Line;
startLocation = trussLocation.GetEndPoint(0);
endLocation = trussLocation.GetEndPoint(1);
//use baseline of truss as the X axis
XYZ diff = endLocation - startLocation;
Vector4 xAxis = new Vector4(new Autodesk.Revit.DB.XYZ(diff.X, diff.Y, diff.Z));
xAxis.Normalize();
//get Z Axis
Vector4 zAxis = Vector4.CrossProduct(xAxis, new Vector4(new Autodesk.Revit.DB.XYZ(0, 0, 1)));
zAxis.Normalize();
//get Y Axis, downward
Vector4 yAxis = Vector4.CrossProduct(xAxis, zAxis);
yAxis.Normalize();
//get original point, first point
m_origin = new Vector4(m_points[0]);
return new Matrix4(xAxis, yAxis, zAxis, m_origin);
}
/// <summary>
/// calculate the matrix use to scale
/// </summary>
/// <returns>maxtrix is use to scale the profile</returns>
public Matrix4 GetScaleMatrix()
{
double xScale = 384 / (m_boundPoints[1].X - m_boundPoints[0].X);
double yScale = 275 / (m_boundPoints[1].Y - m_boundPoints[0].Y);
double factor = xScale <= yScale ? xScale : yScale;
return new Matrix4((double)(factor * 0.85));
}
/// <summary>
/// Get a matrix which can move points to center
/// </summary>
/// <returns>matrix used to move point to center of graphics</returns>
public Matrix4 GetMoveToCenterMatrix()
{
//translate the origin to bound center
Autodesk.Revit.DB.XYZ[] bounds = GetBoundsPoints();
Autodesk.Revit.DB.XYZ min = bounds[0];
Autodesk.Revit.DB.XYZ max = bounds[1];
Autodesk.Revit.DB.XYZ center = new Autodesk.Revit.DB.XYZ((min.X + max.X) / 2, (min.Y + max.Y) / 2, 0);
return new Matrix4(new Vector4(center.X, center.Y, 0));
}
/// <summary>
/// calculate the matrix used to transform 3D to 2D
/// </summary>
/// <returns>maxtrix is use to transform 3d points to 2d</returns>
public Matrix4 Get3DTo2DMatrix()
{
Matrix4 result = Matrix4.Multiply(
m_to2DMatrix.Inverse(), m_moveToCenterMatrix.Inverse());
result = Matrix4.Multiply(result, m_scaleMatrix);
return Matrix4.Multiply(result, new Matrix4(new Vector4(192, 137, 0)));
}
/// <summary>
/// calculate the matrix used to transform 2D to 3D
/// </summary>
/// <returns>maxtrix is use to transform 2d points to 3d</returns>
public Matrix4 Get2DTo3DMatrix()
{
Matrix4 matrix = Matrix4.Multiply(
new Matrix4(new Vector4(-192, -137, 0)), m_scaleMatrix.Inverse());
matrix = Matrix4.Multiply(matrix, m_moveToCenterMatrix);
return Matrix4.Multiply(matrix, m_to2DMatrix);
}
/// <summary>
/// calculate the matrix used to transform 2d points (on pictureBox) to the plane of truss
/// which use to set profile
/// </summary>
/// <returns>maxtrix is use to transform 2d points to the plane of truss</returns>
public Matrix4 Get2DToTrussProfileMatrix()
{
Matrix4 matrix = Matrix4.Multiply(
new Matrix4(new Vector4(-192, -137, 0)), m_scaleMatrix.Inverse());
return Matrix4.Multiply(matrix, m_moveToCenterMatrix);
////downward in picture box, so rotate upward here, y = -y
//Matrix4 upward = new Matrix4(new Vector4(new Autodesk.Revit.DB.XYZ (1, 0, 0)),
// new Vector4(new Autodesk.Revit.DB.XYZ (0, -1, 0)), new Vector4(new Autodesk.Revit.DB.XYZ (0, 0, 1)));
//return Matrix4.Multiply(matrix, upward);
}
/// <summary>
/// Get max and min coordinates of all points
/// </summary>
/// <returns>points array stores the bound of all points</returns>
public Autodesk.Revit.DB.XYZ[] GetBoundsPoints()
{
Matrix4 matrix = m_to2DMatrix;
Matrix4 inverseMatrix = matrix.Inverse();
double minX = 0, maxX = 0, minY = 0, maxY = 0;
bool bFirstPoint = true;
//get the max and min point on the face
foreach (Autodesk.Revit.DB.XYZ point in m_points)
{
Vector4 v = new Vector4(point);
Vector4 v1 = inverseMatrix.Transform(v);
if (bFirstPoint)
{
minX = maxX = v1.X;
minY = maxY = v1.Y;
bFirstPoint = false;
}
else
{
if (v1.X < minX) { minX = v1.X; }
else if (v1.X > maxX) { maxX = v1.X; }
if (v1.Y < minY) { minY = v1.Y; }
else if (v1.Y > maxY) { maxY = v1.Y; }
}
}
//return an array with max and min value of all points
Autodesk.Revit.DB.XYZ[] resultPoints = new Autodesk.Revit.DB.XYZ[2] {
new Autodesk.Revit.DB.XYZ (minX, minY, 0), new Autodesk.Revit.DB.XYZ (maxX, maxY, 0) };
return resultPoints;
}
/// <summary>
/// draw profile of truss in pictureBox
/// </summary>
/// <param name="graphics">form graphic</param>
/// <param name="pen">pen used to draw line in pictureBox</param>
public void Draw2D(Graphics graphics, Pen pen)
{
//draw truss curves
for (int i = 0; i < m_points.Count - 1; i += 2)
{
Autodesk.Revit.DB.XYZ point1 = m_points[i];
Autodesk.Revit.DB.XYZ point2 = m_points[i + 1];
Vector4 v1 = new Vector4(point1);
Vector4 v2 = new Vector4(point2);
v1 = m_transformMatrix.Transform(v1);
v2 = m_transformMatrix.Transform(v2);
graphics.DrawLine(pen, new Point((int)v1.X, (int)v1.Y),
new Point((int)v2.X, (int)v2.Y));
}
//draw selected beam (line) by red pen
DrawSelectedLineRed(graphics);
//draw top chord and bottom chord
m_topChord.Draw2D(graphics, Pens.Red);
m_bottomChord.Draw2D(graphics, Pens.Black);
}
/// <summary>
/// Set profile of truss
/// </summary>
/// <param name="commandData">object which contains reference of Revit Application</param>
public void SetProfile(ExternalCommandData commandData)
{
if (m_topChord.Points.Count < 2)
{ TaskDialog.Show("Truss API", "Haven't drawn top chord"); return; }
else if (m_bottomChord.Points.Count < 2)
{ TaskDialog.Show("Truss API", "Haven't drawn bottom chord"); return; }
Autodesk.Revit.Creation.Document createDoc = commandData.Application.ActiveUIDocument.Document.Create;
Autodesk.Revit.Creation.Application createApp = commandData.Application.Application.Create;
CurveArray curvesTop = createApp.NewCurveArray();
CurveArray curvesBottom = createApp.NewCurveArray();
//get coordinates of top (bottom) chord from lineTool
GetChordPoints(m_topChord, curvesTop, createApp);
GetChordPoints(m_bottomChord, curvesBottom, createApp);
try
{
//set profile by top curve and bottom curve drawn by user in picture box
m_truss.SetProfile(curvesTop, curvesBottom);
}
catch (Exception ex)
{
TaskDialog.Show("Truss API", ex.Message);
}
//re-calculate geometry info after truss profile changed
GetTrussGeometryInfo();
ClearChords();
}
private void GetChordPoints(LineTool chord, CurveArray curves, Autodesk.Revit.Creation.Application createApp)
{
//get coordinates of top chord from lineTool
for (int i = 0; i < chord.Points.Count - 1; i++)
{
Point point = (Point)chord.Points[i];
Point point2 = (Point)chord.Points[i + 1];
Autodesk.Revit.DB.XYZ xyz = new Autodesk.Revit.DB.XYZ(point.X, point.Y, 0);
Autodesk.Revit.DB.XYZ xyz2 = new Autodesk.Revit.DB.XYZ(point2.X, point2.Y, 0);
Vector4 v1 = new Vector4(xyz);
Vector4 v2 = new Vector4(xyz2);
v1 = m_restoreMatrix.Transform(v1);
v2 = m_restoreMatrix.Transform(v2);
try
{
Line line = Line.CreateBound(
new Autodesk.Revit.DB.XYZ(v1.X, v1.Y, v1.Z), new Autodesk.Revit.DB.XYZ(v2.X, v2.Y, v2.Z));
curves.Append(line);
}
catch (System.ArgumentException)
{
TaskDialog.Show("Revit",
"The start point and the end point of the line are too close, please re-draw it.");
ClearChords();
}
}
}
/// <summary>
/// restores truss profile to original
/// </summary>
public void RemoveProfile()
{
m_truss.RemoveProfile();
GetTrussGeometryInfo();
ClearChords();
}
/// <summary>
/// add new point to line tool which used to draw top chord
/// </summary>
/// <param name="x">X coordinate</param>
/// <param name="y">Y coordinate</param>
public void AddTopChordPoint(int x, int y)
{
// doesn't allow to add 2 points near-by
if (m_topChord.Points.Count > 0)
{
Point lastPoint = (Point)m_topChord.Points[m_topChord.Points.Count - 1];
if (Math.Abs(lastPoint.X - x) < 1 ||
Math.Abs(lastPoint.Y - y) < 1)
{
return;
}
}
m_topChord.Points.Add(new Point(x, y));
}
/// <summary>
/// add new point to line tool which used to draw bottom chord
/// </summary>
/// <param name="x">X coordinate</param>
/// <param name="y">Y coordinate</param>
public void AddBottomChordPoint(int x, int y)
{
// doesn't allow to add 2 points near-by
if (m_topChord.Points.Count > 0)
{
Point lastPoint = (Point)m_topChord.Points[m_topChord.Points.Count - 1];
if (Math.Abs(lastPoint.X - x) < 1 ||
Math.Abs(lastPoint.Y - y) < 1)
{
return;
}
}
m_bottomChord.Points.Add(new Point(x, y));
}
/// <summary>
/// add move point to line tool of top chord
/// </summary>
/// <param name="x">X coordinate</param>
/// <param name="y">Y coordinate</param>
public void AddTopChordMovePoint(int x, int y)
{
m_topChord.MovePoint = new Point(x, y);
m_bottomChord.MovePoint = Point.Empty;
}
/// <summary>
/// add move point to line tool of bottom chord
/// </summary>
/// <param name="x">X coordinate</param>
/// <param name="y">Y coordinate</param>
public void AddBottomChordMovePoint(int x, int y)
{
m_bottomChord.MovePoint = new Point(x, y);
m_topChord.MovePoint = Point.Empty;
}
public void ClearMovePoint()
{
m_topChord.MovePoint = Point.Empty;
m_bottomChord.MovePoint = Point.Empty;
}
/// <summary>
/// clear points of top chord and bottom chord
/// </summary>
public void ClearChords()
{
m_topChord.Points.Clear();
m_bottomChord.Points.Clear();
}
/// <summary>
/// Create GraphicsPath object for each curves of truss
/// </summary>
public void CreateGraphicsPath()
{
m_graphicsPaths.Clear();
//create path for all the curves of Truss
for (int i = 0; i < m_points.Count - 1; i += 2)
{
Autodesk.Revit.DB.XYZ point1 = m_points[i];
Autodesk.Revit.DB.XYZ point2 = m_points[i + 1];
Vector4 v1 = new Vector4(point1);
Vector4 v2 = new Vector4(point2);
v1 = m_transformMatrix.Transform(v1);
v2 = m_transformMatrix.Transform(v2);
GraphicsPath path = new GraphicsPath();
path.AddLine(new Point((int)v1.X, (int)v1.Y), new Point((int)v2.X, (int)v2.Y));
m_graphicsPaths.Add(path);
}
}
/// <summary>
/// Judge which truss member has been selected via location of mouse
/// </summary>
/// <param name="x">X coordinate of mouse location</param>
/// <param name="y">Y coordinate of mouse location</param>
/// <returns>index of selected member</returns>
public int SelectTrussMember(int x, int y)
{
Point point = new Point(x, y);
for (int i = 0; i < m_graphicsPaths.Count; i++)
{
GraphicsPath path = (GraphicsPath)m_graphicsPaths[i];
if (path.IsOutlineVisible(point, Pens.Blue))
{
m_selectMemberIndex = i;
return m_selectMemberIndex;
}
}
m_selectMemberIndex = -1;
return m_selectMemberIndex;
}
/// <summary>
/// Draw selected line (beam) by red pen
/// </summary>
/// <param name="graphics">graphics of picture box</param>
public void DrawSelectedLineRed(Graphics graphics)
{
Pen redPen = new Pen(System.Drawing.Color.Red, (float)2.0);
//draw the selected beam as red line
if (m_selectMemberIndex != -1)
{
GraphicsPath selectPath = (GraphicsPath)(m_graphicsPaths[m_selectMemberIndex]);
PointF startPointOfSelectedLine = (PointF)(selectPath.PathPoints.GetValue(0));
PointF endPointOfSelectedLine = (PointF)(selectPath.PathPoints.GetValue(1));
graphics.DrawLine(redPen, startPointOfSelectedLine, endPointOfSelectedLine);
}
//draw clicked beam red
if (m_clickMemberIndex != -1)
{
GraphicsPath selectPath = (GraphicsPath)(m_graphicsPaths[m_clickMemberIndex]);
PointF startPointOfSelectedLine = (PointF)(selectPath.PathPoints.GetValue(0));
PointF endPointOfSelectedLine = (PointF)(selectPath.PathPoints.GetValue(1));
graphics.DrawLine(redPen, startPointOfSelectedLine, endPointOfSelectedLine);
}
}
/// <summary>
/// Get selected beam (truss member) by select index
/// </summary>
/// <param name="commandData">object which contains reference of Revit Application</param>
/// <returns>index of selected member</returns>
public FamilyInstance GetSelectedBeam(ExternalCommandData commandData)
{
m_clickMemberIndex = m_selectMemberIndex;
Autodesk.Revit.DB.ElementId id = null;
List<ElementId> idSet = m_truss.Members as List<ElementId>;
IEnumerator iter = idSet.GetEnumerator();
iter.Reset();
int i = 0;
while (iter.MoveNext())
{
if (i == m_selectMemberIndex)
{
id = iter.Current as Autodesk.Revit.DB.ElementId;
break;
}
i++;
}
return (FamilyInstance)commandData.Application.ActiveUIDocument.Document.GetElement(id);
}
/// <summary>
/// Reset index and clear line tool
/// </summary>
public void Reset()
{
m_clickMemberIndex = -1;
m_selectMemberIndex = -1;
m_topChord.Points.Clear();
m_bottomChord.Points.Clear();
}
}
}
版权所有 :无锡模信建筑科技有限公司 苏ICP备2021028830号-1 BIM建模|BIM技术应用|BIM软件开发
联系地址:江苏省无锡市新吴区龙山路4号B座705 手机:18761516598