Creating model lines

Hi,

I’m putting together a Python routine to draw some lines in the project environment.

The routine will draw some 3D lines mode lines in the project environment, not the family editor.

I’m struggling to work out which class is the one to use, the ones I try are cause exceptions as they are lines for family creation.

I believe you need to create a new unbound line, and then from that create NewModelCurve.

Is that correct, I’d appreciate an example of how to do this.

There is an example in the docs ;

http://www.revitapidocs.com/2017/b880c4d7-9841-e44e-2a1c-36fefe274e2e.htm

OK, thanks, I’ve got that already.

I guess the problem is I cannot create the SketchPlane for the model line in the model environment, I can do it easily in the family environment with:

plane = app.Create.NewPlane(normal, origin)
skplane = doc.FamilyCreate.NewSketchPlane(plane)

But cant find the model enviroment equivalent of doc.FamilyCreate.NewSketchPlane(plane), guess it would be doc.Create.NewSketchPlane(plane) - but that does not work.

This is my code:

newline = Autodesk.Revit.DB.Line.CreateBound(XYZ(0,0,0), XYZ(1000,1000,300))
origin = XYZ.Zero
normal = XYZ.BasisZ
 
plane = app.Create.NewPlane(normal, origin)
skplane = doc.Create.NewSketchPlane(plane)

#Assign your output to the OUT variable.
OUT = [levelid, newline, plane, plane, skplane]

and it generates an error with AttributeError: ‘Document’ object has no attribute ‘NewSketchPlane’

I feel like I’m missing something obvious…

SketchPlanes are created directly from the SketchPlane class:

http://www.revitapidocs.com/2017/51d4403a-b78d-7527-f3bc-463d8044d0d4.htm

something like this: sp1 = SketchPlane.Create(doc1, plane1)

OK, thanks that worked.

But I get transaction issues. The first time I run the code it works fine. If it run it again I get the error:

_Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed. _
Starting a new transaction is not permitted. It could be because another transaction already started and has not been completed yet, or the document is in a state in which it cannot start a new transaction (e.g. during failure handling or a read-only mode, which could be either permanent or temporary).

I can only run it again if I restart Dynamo.

If I add a transaction into the code it state generates an exception stating that it cannot start a transaction as one is already in progress.

This is my code:

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference("RevitAPIUI")
from Autodesk.Revit.UI import *

from math import atan2, radians, cos, sin, degrees

import time

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager

from System.Collections.Generic import *

import sys
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
adoc = doc.ActiveView


#The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

levelid = UnwrapElement(IN[0])

t = Transaction(doc, "Random Move")
t.Start()

newline = Autodesk.Revit.DB.Line.CreateBound(XYZ(0,0,0), XYZ(1000,1000,300))
origin = XYZ.Zero
normal = XYZ.BasisZ
 
plane = app.Create.NewPlane(normal, origin)
skplane = SketchPlane.Create(doc, plane)

t.Commit

#Assign your output to the OUT variable.
OUT = [levelid, newline, plane, plane, skplane]

Should not all of this be handled by Dynamo’s transaction?

Thanks.

Maybe it’s because you are missing the parentheses after t.Commit

It’s probably better to use Dynamos transaction manager :

import clr

# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# Get the document
doc = DocumentManager.Instance.CurrentDBDocument

# "Start" the transaction
TransactionManager.Instance.EnsureInTransaction(doc)

# Create a reference point (requires a transaction)
refPt = doc.FamilyCreate.NewReferencePoint(XYZ(0, 0, 0))

# "End" the transaction
TransactionManager.Instance.TransactionTaskDone()
1 Like

Perfect - that worked!

Thanks for your help.