I hope this is the cure for all our pain:
#region Namespaces
using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;
//using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;
using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;
using System.Diagnostics;
#endregion
namespace RevitAddinCS2
{
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class ExtCmd : IExternalCommand
{
#region Cached Variables
private static ExternalCommandData _cachedCmdData;
public static UIApplication CachedUiApp
{
get
{
return _cachedCmdData.Application;
}
}
public static RvtApplication CachedApp
{
get
{
return CachedUiApp.Application;
}
}
public static RvtDocument CachedDoc
{
get
{
return CachedUiApp.ActiveUIDocument.Document;
}
}
#endregion
#region IExternalCommand Members
public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
//Variable declaration
_cachedCmdData = commandData;
// To catch Exception
try
{
TaskDialog.Show("Revit", "Select column");
Reference r = CachedUiApp.ActiveUIDocument.Selection.PickObject(ObjectType.Element);
ElementId e = r.ElementId;
Autodesk.Revit.DB.Element column = CachedDoc.GetElement(e);
FamilyInstance familyInstance = column as FamilyInstance;
double length;
Rebar r1;
BoundingBoxXYZ bb = null;
if (null != familyInstance && familyInstance.StructuralType == Autodesk.Revit.DB.Structure.StructuralType.Column)
{
LocationPoint location = column.Location as LocationPoint;
XYZ origin = location.Point;
bb = column.get_BoundingBox(null);
double minX = bb.Min.X;
double maxX = bb.Max.X;
length = (maxX - minX);
double minY = bb.Min.Y;
double maxY = bb.Max.Y;
double width = (maxY - minY);
double height = (bb.Max.Z - bb.Min.Z);
double minZ = bb.Min.Z;
#region Horizontal Points (Top-Bottom)
//XYZ point1 = new XYZ(maxX - .177, maxY - .177, minZ);
//XYZ point2 = new XYZ(minX + .177, maxY - .177, minZ);
//XYZ point3 = new XYZ(minX + .177, minY + .177, minZ);
//XYZ point4 = new XYZ(maxX - .177, minY + .177, minZ);
//XYZ normal = new XYZ(0, 0, 1);
#endregion
#region Vertical Points (Front-Back)
//XYZ point1 = new XYZ(maxX - .177, minY + .177, minZ + height - .177);
//XYZ point2 = new XYZ(minX + .177, minY + .177, minZ + height - .177);
//XYZ point3 = new XYZ(minX + .177, minY + .177, minZ + .177);
//XYZ point4 = new XYZ(maxX - .177, minY + .177, minZ + .177);
//XYZ normal = new XYZ(0, 1, 0);
#endregion
#region Vertical Points (Right-Left)
XYZ point1 = new XYZ(maxX - .177, maxY - .177, minZ + height - .177);
XYZ point2 = new XYZ(maxX - .177, minY + .177, minZ + height - .177);
XYZ point3 = new XYZ(maxX - .177, minY + .177, minZ + .177);
XYZ point4 = new XYZ(maxX - .177, maxY - .177, minZ + .177);
XYZ normal = new XYZ(1, 0, 0);
#endregion
Line lr1 = Line.CreateBound(point1, point2);
Line lr2 = Line.CreateBound(point2, point3);
Line lr3 = Line.CreateBound(point3, point4);
Line lr4 = Line.CreateBound(point4, point1);
RebarBarType rebarType = null;
// Rebar Type
using (Transaction t = new Transaction(CachedDoc, "RebarBar Type"))
{
t.Start();
rebarType = RebarBarType.Create(CachedDoc);
rebarType.BarDiameter = 0.025;
rebarType.StirrupTieBendDiameter = .1;
t.Commit();
}
// Create the line rebar
IList<Curve> curves = new List<Curve>();
curves.Add(lr1);
curves.Add(lr2);
curves.Add(lr3);
curves.Add(lr4);
RebarHookOrientation ho = RebarHookOrientation.Right;
RebarHookOrientation hi = RebarHookOrientation.Left;
using (Transaction t = new Transaction(CachedDoc, "RebarBar Type"))
{
t.Start();
r1 = Rebar.CreateFromCurves(CachedDoc,
RebarStyle.StirrupTie,
rebarType,
null,
null,
column,
normal,
curves, ho, hi, true, true);
t.Commit();
}
}
else
{
TaskDialog.Show("Revit", "Selected Item is not column");
}
return Result.Succeeded;
}
catch (Exception e)
{
TaskDialog.Show("Revit", e.Message + " " + e.Data);
return Result.Failed;
}
}
#endregion
}
}
If this reply satisfies your need please don't forget to mark it as an answer.
¯\_(ツ)_/¯
Let it work like a charm.
Mustafa Salaheldin


Digital Integration Manager, DuPod
Facebook |
Twitter |
LinkedIn