3D Model Line

3D Model Line

Dale.Bartlett
Collaborator Collaborator
8,360 Views
6 Replies
Message 1 of 7

3D Model Line

Dale.Bartlett
Collaborator
Collaborator

The simplest things seem to baffle me. I wish to draw a 3D Model Line, for example from (0,0,0) to (1,0,1) to (1,1,2). How hard can it be? Dale




______________
Yes, I'm Satoshi.
0 Likes
Accepted solutions (1)
8,361 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable
You'll need to create a SketchPlane for each non-coplanar line segment. It's a pain in the butt. I ended up creating a static helper class that does all the heavy lifting and allows the simple syntax like LineModeler.DrawLines ()
0 Likes
Message 3 of 7

Dale.Bartlett
Collaborator
Collaborator

I suspected that (****!). Given the 2 points to create a line, does it matter where the third point is specified to create the Sketch Plane? Dale




______________
Yes, I'm Satoshi.
0 Likes
Message 4 of 7

Revitalizer
Advisor
Advisor

Hi,

 

I usuall set the third point to (-1000,-1000,0).

 

static void DrawLine(UIApplication app, Document doc, Line line)
{
    // define a triangle
    CurveArray ca = new CurveArray();
    XYZ origin = new XYZ(-1000, -1000, 0);
    ca.Append(line);
    ca.Append(Line.CreateBound(line.GetEndPoint(1), origin));
    ca.Append(Line.CreateBound(origin, line.GetEndPoint(0)));

    Plane plane =  app.Application.Create.NewPlane(ca);
    SketchPlane skp = SketchPlane.Create(doc, plane);
    ModelCurve mc = doc.Create.NewModelCurve(line, skp);
}

 

 

Revitaizer




Rudolf Honke
Software Developer
Mensch und Maschine





0 Likes
Message 5 of 7

Dale.Bartlett
Collaborator
Collaborator
Accepted solution

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.
0 Likes
Message 6 of 7

Dale.Bartlett
Collaborator
Collaborator
Ok just discovered Jeremy's updates: https://github.com/jeremytammik/the_building_coder_samples/blob/master/BuildingCoder/BuildingCoder/C...
No plagiarism intended. Dale



______________
Yes, I'm Satoshi.
0 Likes
Message 7 of 7

Anonymous
Not applicable

Yet again, even after countless annual updates, Revit fails to provide the basic functionality that Sketchup is able to do without any problems.