using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.ApplicationServices;
namespace ChangChunTest
{
[TransactionAttribute(TransactionMode.Manual)]
class Cube_cmd : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
double a = 1000 / 304.8;//长度
Extrusion extrusion = null;
using (Transaction trans = new Transaction(doc, "Cube"))
{
trans.Start();
// 定义轮廓集合
CurveArrArray curveArrArray = new CurveArrArray();
CurveArray curveArray1 = new CurveArray();
CurveArray curveArray2 = new CurveArray();
CurveArray curveArray3 = new CurveArray();
// 创建平面轮廓
XYZ p0 = XYZ.Zero;
XYZ p1 = new XYZ(a, 0, 0);
XYZ p2 = new XYZ(a, a, 0);
XYZ p3 = new XYZ(0, a, 0);
Line line1 = Line.CreateBound(p0, p1);
Line line2 = Line.CreateBound(p1, p2);
Line line3 = Line.CreateBound(p2, p3);
Line line4 = Line.CreateBound(p3, p0);
curveArray1.Append(line1);
curveArray1.Append(line2);
curveArray1.Append(line3);
curveArray1.Append(line4);
curveArrArray.Append(curveArray1);
//定义草图平面
Plane plane = Plane.CreateByNormalAndOrigin(new XYZ(0, 0, 1), XYZ.Zero);
SketchPlane sketchPlane = SketchPlane.Create(doc, plane);
// 创建固体拉伸
extrusion = doc.FamilyCreate.NewExtrusion(true, curveArrArray, sketchPlane, 1200/304.8);
trans.Commit();
}
using (Transaction trans2 = new Transaction(doc, "Cube-DIM"))
{
trans2.Start();
FamilyManager m_familyMgr = doc.FamilyManager;
bool isInstance = false;
//FamilyParameter HparamTw_Y = m_familyMgr.AddParameter("长方体Y方向宽度", BuiltInParameterGroup.PG_GEOMETRY, ParameterType.Length, isInstance);
//m_familyMgr.Set(HparamTw_Y, a);//Y设置初始值
//FamilyParameter HparamTw_X = m_familyMgr.AddParameter("长方体X方向宽度", BuiltInParameterGroup.PG_GEOMETRY, ParameterType.Length, isInstance);
//m_familyMgr.Set(HparamTw_X, a);//X设置初始值
XYZ x1 = new XYZ(1, 0, 0);
XYZ x2 = new XYZ(-1, 0, 0);
XYZ y1 = new XYZ(0, 1, 0);
XYZ y2 = new XYZ(0, -1, 0);
Line line_x1 = null;//对应向量(1,0,0)
Line line_x2 = null;//对应向量(-1,0,0;
Line line_y1 = null;//对应向量(0,1,0)
Line line_y2 = null;//对应向量(0,-1,0)
ReferenceArray references1 = new ReferenceArray();
ReferenceArray references2 = new ReferenceArray();
foreach (CurveArray item in extrusion.Sketch.Profile)
{
foreach (var curve in item)
{
if (curve is Line)
{
Line line = curve as Line;
if (line.Direction.IsAlmostEqualTo(x2, 0.01))
{
line_x2 = line;
var lineX2 = doc.GetElement(line_x2.Reference) as ModelLine;
references2.Append(lineX2.GeometryCurve.GetEndPointReference(1));
}
if (line.Direction.IsAlmostEqualTo(x1, 0.01))
{
line_x1 = line;
var lineX1 = doc.GetElement(line_x1.Reference) as ModelLine;
}
if (line.Direction.IsAlmostEqualTo(y1, 0.01))
{
line_y1 = line;
var lineY1 = doc.GetElement(line_y1.Reference) as ModelLine;
references1.Append(lineY1.GeometryCurve.GetEndPointReference(0));
references2.Append(lineY1.GeometryCurve.GetEndPointReference(1));
}
if (line.Direction.IsAlmostEqualTo(y2, 0.01))
{
line_y2= line;
var lineY2 = doc.GetElement(line_y1.Reference) as ModelLine;
references1.Append(lineY2.GeometryCurve.GetEndPointReference(1));
}
}
}
}
Dimension dimensionY = doc.FamilyCreate.NewDimension(doc.ActiveView, line_y1, references1);
dimensionY.FamilyLabel = m_familyMgr.get_Parameter("长方体Y方向宽度");
Dimension dimensionX = doc.FamilyCreate.NewDimension(doc.ActiveView, line_x1, references2);
dimensionX.FamilyLabel = m_familyMgr.get_Parameter("长方体X方向宽度"); ;
trans2.Commit();
}
return Result.Succeeded;
}
}
}