That proved to be time-consuming to resolve, so here is something for the community. I lost track of who to credit, but this is based on Jeremy's work from 2008, updated for 2014. Hopefully useful. Dale
// http://thebuildingcoder.typepad.com/blog/2008/11/model-line-creation.html
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 = pUiapp.Application.Create.NewPlane(norm, p);
SketchPlane skPlane = SketchPlane.Create(doc, plane);
return skPlane;
}
public static Result Create3DModelLine(UIApplication pUiapp, XYZ p, XYZ q, string pstrLineStyle, bool pblnIsFamily)
{
Document doc = pUiapp.ActiveUIDocument.Document;
Result lresResult = Result.Failed;
using (Transaction tr = new Transaction(doc, "Create3DModelLine"))
{
tr.Start();
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.");
}
ModelCurve mCurve = null;
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);
if (e.Name == pstrLineStyle)
{
mCurve.LineStyle = e;
break;
}
}
tr.Commit();
lresResult = Result.Succeeded;
}
catch (Autodesk.Revit.Exceptions.ExternalApplicationException ex)
{
MessageBox.Show(ex.Source + Environment.NewLine + ex.StackTrace + Environment.NewLine + ex.Message);
// tr.RollBack();
}
}
return lresResult;
}
// usage
// all in feet remember
// line 1
XYZ StartPt = new XYZ(0, 0, 0);
XYZ EndPt = new XYZ(10, 0, 10);
string lstrLineStyle = "<Hidden>"; // system linestyle guaranteed to exist
lresResult = Creator.Create3DModelLine(pUiapp, StartPt, EndPt, lstrLineStyle, true);
// line 2
StartPt = EndPt;
EndPt = new XYZ(10, 10, 20);
lresResult = Creator.Create3DModelLine(pUiapp, StartPt, EndPt, lstrLineStyle, true);
______________
Yes, I'm Satoshi.