Created
June 20, 2024 05:59
-
-
Save chuongmep/a2c4d0faaa657a4f882c4b02343d83ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Autodesk.Revit.Attributes; | |
using Autodesk.Revit.DB; | |
using Autodesk.Revit.UI; | |
using Autodesk.Revit.UI.Selection; | |
using Autodesk.Revit.DB.Analysis; | |
[Transaction(TransactionMode.Manual)] | |
public class PickCurveAndCreateDirectShape : IExternalCommand | |
{ | |
public Result Execute( | |
ExternalCommandData commandData, | |
ref string message, | |
ElementSet elements) | |
{ | |
UIApplication uiapp = commandData.Application; | |
UIDocument uidoc = uiapp.ActiveUIDocument; | |
Document doc = uidoc.Document; | |
try | |
{ | |
// Pick a curve | |
Reference pickedRef = uidoc.Selection.PickObject(ObjectType.Element, new CurveSelectionFilter(), "Select a curve"); | |
Element element = doc.GetElement(pickedRef); | |
PathOfTravel curveElement = element as PathOfTravel; | |
IList<Curve> pickedCurves = curveElement.GetCurves(); | |
// Define the height of the extrusion | |
double extrusionHeight = 100.0; | |
double width = 5.0; // Define the width of the rectangle | |
List<Solid> solids = new List<Solid>(); | |
foreach (Curve pickedCurve in pickedCurves) | |
{ | |
// Create the offset profile for each curve | |
List<Curve> profile = CreateOffsetProfile(pickedCurve, width); | |
CurveLoop profileLoop = CurveLoop.Create(profile); | |
List<CurveLoop> curveLoops = new List<CurveLoop> { profileLoop }; | |
// Create the solid geometry | |
SolidOptions options = new SolidOptions(ElementId.InvalidElementId, ElementId.InvalidElementId); | |
Solid solid = GeometryCreationUtilities.CreateExtrusionGeometry(curveLoops, XYZ.BasisZ, extrusionHeight); | |
solids.Add(solid); | |
} | |
using Transaction tx = new Transaction(doc); | |
tx.Start("Create DirectShape"); | |
// Create the DirectShape | |
DirectShape ds = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_GenericModel)); | |
ds.SetShape(solids.Cast<GeometryObject>().ToArray()); | |
ds.Name = "MyDirectShape"; | |
// Commit the transaction | |
tx.Commit(); | |
// MessageBox.Show("DirectShape created successfully."); | |
return Result.Succeeded; | |
} | |
catch (Exception ex) | |
{ | |
message = ex.ToString(); | |
return Result.Failed; | |
} | |
} | |
private List<Curve> CreateOffsetProfile(Curve curve, double width) | |
{ | |
XYZ startPoint = curve.GetEndPoint(0); | |
XYZ endPoint = curve.GetEndPoint(1); | |
XYZ direction = (endPoint - startPoint).Normalize(); | |
XYZ perpendicular = XYZ.BasisZ.CrossProduct(direction).Normalize(); | |
XYZ offset = perpendicular * (width / 2.0); | |
// Create the offset points | |
XYZ offsetStart1 = startPoint - offset; | |
XYZ offsetEnd1 = endPoint - offset; | |
XYZ offsetStart2 = startPoint + offset; | |
XYZ offsetEnd2 = endPoint + offset; | |
List<Curve> profile = new List<Curve> | |
{ | |
Line.CreateBound(offsetStart1, offsetEnd1), | |
Line.CreateBound(offsetEnd1, offsetEnd2), | |
Line.CreateBound(offsetEnd2, offsetStart2), | |
Line.CreateBound(offsetStart2, offsetStart1) | |
}; | |
return profile; | |
} | |
} | |
public class CurveSelectionFilter : ISelectionFilter | |
{ | |
public bool AllowElement(Element elem) | |
{ | |
return elem is PathOfTravel; | |
} | |
public bool AllowReference(Reference reference, XYZ position) | |
{ | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment