Client/Desktop/KMBIM3.0/23.10.18/Utils/Creator.cs

103 lines
3.5 KiB
C#

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace KMBIM.Revit.Tools.Utils
{
class Creator
{
static SketchPlane NewSketchPlanePassLine(UIApplication pUiapp, Line line)
{
Document doc = pUiapp.ActiveUIDocument.Document;
XYZ p = line.GetEndPoint(0);
XYZ q = line.GetEndPoint(1);
XYZ norm;
if (p.X == q.X)
{
norm = XYZ.BasisX;
}
else if (p.Y == q.Y)
{
norm = XYZ.BasisY;
}
else
{
norm = XYZ.BasisZ;
}
Plane plane = Plane.CreateByNormalAndOrigin(norm, p);
SketchPlane skPlane = SketchPlane.Create(doc, plane);
return skPlane;
}
public static ModelCurve Create3DModelLine(UIApplication pUiapp, XYZ p, XYZ q, int builtInCategory, bool pblnIsFamily = false)
{
Document doc = pUiapp.ActiveUIDocument.Document;
ModelCurve mCurve = null;
try
{
if (p.IsAlmostEqualTo(q))
{
throw new System.ArgumentException("Expected two different points.");
}
Line line = Line.CreateBound(p, q);
if (null == line)
{
throw new Exception("Geometry line creation failed.");
}
if (pblnIsFamily)
{
mCurve = doc.FamilyCreate.NewModelCurve(line, NewSketchPlanePassLine(pUiapp, line)); // not needed , pblnIsFamily));
}
else
{
mCurve = doc.Create.NewModelCurve(line, NewSketchPlanePassLine(pUiapp, line)); // not needed , pblnIsFamily));
}
// set linestyle
ICollection<ElementId> styles = mCurve.GetLineStyleIds();
foreach (ElementId eid in styles)
{
Element e = doc.GetElement(eid);
GraphicsStyle grStyle = e as GraphicsStyle;
if (grStyle != null)
{
if (grStyle.GraphicsStyleCategory.Id.IntegerValue == builtInCategory)
{
GraphicsStyle gs = mCurve.LineStyle as GraphicsStyle;
mCurve.LineStyle = e;
break;
}
}
}
}
catch (Autodesk.Revit.Exceptions.ExternalApplicationException ex)
{
MessageBox.Show(ex.Source + Environment.NewLine + ex.StackTrace + Environment.NewLine + ex.Message);
}
return mCurve;
}
public static ModelCurve Create3DModelLineTransaction(UIApplication pUiapp, XYZ p, XYZ q, int builtInCategory, bool pblnIsFamily = false)
{
Document doc = pUiapp.ActiveUIDocument.Document;
ModelCurve mCurve = null;
using (Transaction tr = new Transaction(doc, "Create3DModelLine"))
{
tr.Start();
mCurve=Create3DModelLine(pUiapp, p, q, builtInCategory, pblnIsFamily);
tr.Commit();
}
return mCurve;
}
}
}