Use revit api to draw an example of a duct that is perpendicular to the duct

  • 2020-06-15 10:05:42
  • OfStack


/// <summary>
///  Select duct and duct outside 1 Point, draw the duct perpendicular to the duct. 
/// </summary>
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class cmd : IExternalCommand
{
    public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elements)
    {
        UIDocument uiDoc = cmdData.Application.ActiveUIDocument;
        UIApplication uiApp = cmdData.Application;
        Document doc = uiDoc.Document;
        Selection selection = uiDoc.Selection;
        Transaction ts = new Transaction(doc, "https://www.ofstack.com");
        ts.Start();
        // get 1 Type of duct 
        DuctType ductType = null;
        FilteredElementCollector collector = new FilteredElementCollector(doc);
        collector.OfClass(typeof(DuctType));
        foreach (Element el in collector.ToElements())
        {
            ductType = el as DuctType;
            break;
        }
        //
        Reference refDuct = selection.PickObject(ObjectType.Element, " choose 1 Root duct: ");
        Duct duct = doc.GetElement(refDuct) as Duct;
        XYZ xyz = selection.PickPoint();// choose 1 point 
        LocationCurve lCurve = duct.Location as LocationCurve;
        IntersectionResult interResult = lCurve.Curve.Project(xyz);// The projection of a point onto a line 
        if (interResult != null)
        {
            doc.Create.NewDuct(xyz, interResult.XYZPoint, ductType);
        }
        ts.Commit();
        return Result.Succeeded;
    }
}


Related articles: