118 lines
3.5 KiB
C#
118 lines
3.5 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
|
|||
|
|
using Autodesk.Revit.Attributes;
|
|||
|
|
using Autodesk.Revit.DB;
|
|||
|
|
using Autodesk.Revit.UI;
|
|||
|
|
|
|||
|
|
using RevitJigSample.ExternalGraphics;
|
|||
|
|
|
|||
|
|
namespace RevitJigSample
|
|||
|
|
{
|
|||
|
|
[Transaction(TransactionMode.Manual)]
|
|||
|
|
[Regeneration(RegenerationOption.Manual)]
|
|||
|
|
public class RectangleJigCommand : IExternalCommand
|
|||
|
|
{
|
|||
|
|
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
|
|||
|
|
{
|
|||
|
|
if (commandData.Application.ActiveUIDocument == null)
|
|||
|
|
{
|
|||
|
|
return Result.Succeeded;
|
|||
|
|
}
|
|||
|
|
UIDocument uiDocument = commandData.Application.ActiveUIDocument;
|
|||
|
|
Document document = uiDocument.Document;
|
|||
|
|
XYZ p1 = null;
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
p1 = uiDocument.Selection.PickPoint("Pick Base Point:");
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
//
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (p1 == null)
|
|||
|
|
{
|
|||
|
|
return Result.Succeeded;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
XYZ p3 = null;
|
|||
|
|
RectangleJig rectangleJig = null;
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
rectangleJig = new RectangleJig(commandData.Application) { DrawingServer = { BasePoint = p1 } };
|
|||
|
|
rectangleJig.DrawJig();
|
|||
|
|
|
|||
|
|
p3 = uiDocument.Selection.PickPoint("Pick Next Point:");
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
//
|
|||
|
|
}
|
|||
|
|
finally
|
|||
|
|
{
|
|||
|
|
rectangleJig?.Dispose();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (p3 == null)
|
|||
|
|
{
|
|||
|
|
return Result.Succeeded;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
XYZ mpt = (p1 + p3) * 0.5;
|
|||
|
|
View currView = document.ActiveView;
|
|||
|
|
Plane plane = Plane.CreateByNormalAndOrigin(currView.RightDirection, mpt);
|
|||
|
|
Transform mirrorMat = Transform.CreateReflection(plane);
|
|||
|
|
|
|||
|
|
XYZ p2 = mirrorMat.OfPoint(p1);
|
|||
|
|
XYZ p4 = mirrorMat.OfPoint(p3);
|
|||
|
|
|
|||
|
|
List<XYZ> points = new List<XYZ> { p1, p2, p3, p4, p1 };
|
|||
|
|
using (Transaction trans = new Transaction(document, "Draw Rectangle"))
|
|||
|
|
{
|
|||
|
|
trans.Start();
|
|||
|
|
XYZ lpt = points[0];
|
|||
|
|
for (int k = 1; k < points.Count; k++)
|
|||
|
|
{
|
|||
|
|
XYZ cpt = points[k];
|
|||
|
|
if (lpt.DistanceTo(cpt) > 0.001)
|
|||
|
|
{
|
|||
|
|
CreateModelLine(document, lpt, cpt);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
lpt = cpt;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
trans.Commit();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return Result.Succeeded;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static ModelCurve CreateModelLine(Document doc, XYZ p, XYZ q)
|
|||
|
|
{
|
|||
|
|
if (p.DistanceTo(q) < doc.Application.ShortCurveTolerance)
|
|||
|
|
{
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
XYZ v = q - p;
|
|||
|
|
double dxy = Math.Abs(v.X) + Math.Abs(v.Y);
|
|||
|
|
XYZ w = (dxy > doc.Application.ShortCurveTolerance)
|
|||
|
|
? XYZ.BasisZ
|
|||
|
|
: XYZ.BasisY;
|
|||
|
|
|
|||
|
|
XYZ norm = v.CrossProduct(w)
|
|||
|
|
.Normalize();
|
|||
|
|
Plane plane = Plane.CreateByNormalAndOrigin(norm, p);
|
|||
|
|
SketchPlane sketchPlane = SketchPlane.Create(doc, plane);
|
|||
|
|
Line line = Line.CreateBound(p, q);
|
|||
|
|
ModelCurve curve = doc.IsFamilyDocument
|
|||
|
|
? doc.FamilyCreate.NewModelCurve(line, sketchPlane)
|
|||
|
|
: doc.Create.NewModelCurve(line, sketchPlane);
|
|||
|
|
|
|||
|
|
return curve;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|