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 Ellipse_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;//xRadius
double b = 600/ 304.8;//yRadius
Revolution revolution = null;
using (Transaction trans = new Transaction(doc, "Ellipse"))
{
trans.Start();
// 定義旋轉的輪廓
CurveArrArray curveArrArray = new CurveArrArray();
CurveArray curveArray = new CurveArray();
// 定義點
XYZ p0 = new XYZ(0, 0, 0);//圓?點
XYZ p1 = new XYZ(1, 0, 0);//x單位向量
XYZ p2 = new XYZ(0, 1, 0);//y單位向量
XYZ p3 = new XYZ(a, 0, 0);
XYZ p4 = new XYZ(0,b,0);
XYZ pp = new XYZ(-10, 0, 0);
Line axis = Line.CreateBound(XYZ.Zero, pp);// 旋轉軸
Line line1 = Line.CreateBound(p0, p3);//H橫切高度
Curve ellipse = Ellipse.CreateCurve(p0, a, b, p1, p2, 0, Math.PI / 2);
Line line2 = Line.CreateBound(p4, p0);//WR赤道半徑
curveArray.Append(line1);
curveArray.Append(ellipse);
curveArray.Append(line2);
curveArrArray.Append(curveArray);
Plane plane= Plane.CreateByNormalAndOrigin(new XYZ(0, 0, 1), XYZ.Zero);
SketchPlane sketchPlane = SketchPlane.Create(doc, plane);
revolution = doc.FamilyCreate.NewRevolution(true, curveArrArray, sketchPlane, axis, -Math.PI, Math.PI);
trans.Commit();
}
using (Transaction trans2 = new Transaction(doc, "Ellipse-DIM-H"))
{
trans2.Start();
XYZ x1 = new XYZ(1, 0, 0);//x單位向量
XYZ y1 = new XYZ(0, -1, 0);//x單位向量
Reference refe1 = null;
Reference refe2 = null;
FamilyManager m_familyMgr = doc.FamilyManager;
bool isInstance = false;
FamilyParameter HparamTwH= m_familyMgr.AddParameter("赤道界面高度2", BuiltInParameterGroup.PG_GEOMETRY, ParameterType.Length, isInstance);
// FamilyParameter HparamTwR = m_familyMgr.AddParameter("極半徑長度", BuiltInParameterGroup.PG_GEOMETRY, ParameterType.Length, isInstance);
Line line_x = null;
Line line_y = null;
Ellipse ellipse = null;
ReferenceArray referencesH = new ReferenceArray();
// ReferenceArray referencesR = new ReferenceArray();
foreach (CurveArray item in revolution.Sketch.Profile)
{
foreach (var curve in item)
{
if (curve is Line)
{
Line line = curve as Line;
if ( Convert.ToInt32( line.Direction.X )== 1)
{
line_x = line;
var lineX = doc.GetElement (line.Reference) as ModelLine;
referencesH.Append(lineX.GeometryCurve.Reference);
// TaskDialog.Show("info",lineX.ToString());
}
if (Convert.ToInt32(line.Direction.Y) == -1)
{
line_y = line;
var lineY = doc.GetElement(line.Reference) as ModelLine;
// referencesR.Append(lineY.GeometryCurve.Reference);
}
}
else if (curve is Ellipse)
{
ellipse = curve as Ellipse;
var model_ellipse = doc.GetElement(ellipse.Reference) as ModelEllipse;
referencesH.Append(model_ellipse.GeometryCurve.GetEndPointReference(0));
// referencesR.Append(model_ellipse.GeometryCurve.GetEndPointReference(0));
}
}
}
Dimension dimension_x = doc.FamilyCreate.NewDimension(doc.ActiveView, line_x, referencesH);
// Dimension dimension_y = doc.FamilyCreate.NewDimension(doc.ActiveView, line_y, referencesR);
dimension_x.FamilyLabel = HparamTwH;
// dimension_y.FamilyLabel = HparamTwR;
trans2.Commit();
}
using (Transaction trans3 = new Transaction(doc, "Ellipse-DIM-R"))
{
trans3.Start();
XYZ x1 = new XYZ(1, 0, 0);//x單位向量
XYZ y1 = new XYZ(0, -1, 0);//x單位向量
FamilyManager m_familyMgr = doc.FamilyManager;
bool isInstance = false;
FamilyParameter HparamTwR = m_familyMgr.AddParameter("極半徑長度1", BuiltInParameterGroup.PG_GEOMETRY, ParameterType.Length, isInstance);
Line line_y = null;
Ellipse ellipse = null;
ReferenceArray referencesR = new ReferenceArray();
foreach (CurveArray item in revolution.Sketch.Profile)
{
foreach (var curve in item)
{
if (curve is Line)
{
Line line = curve as Line;
if (Convert.ToInt32(line.Direction.Y) == -1)
{
line_y = line;
var lineY = doc.GetElement(line.Reference) as ModelLine;
referencesR.Append(lineY.GeometryCurve.Reference);
}
}
else if (curve is Ellipse)
{
ellipse = curve as Ellipse;
var model_ellipse = doc.GetElement(ellipse.Reference) as ModelEllipse;
referencesR.Append(model_ellipse.GeometryCurve.GetEndPointReference(0));
}
}
}
Dimension dimension_R = doc.FamilyCreate.NewDimension(doc.ActiveView, line_y, referencesR);
dimension_R.FamilyLabel = HparamTwR;
trans3.Commit();
}
return Result.Succeeded;
}
}
}