This works fine in IronPython but the ToString() breaks in Py3.
How do I write it?
The name is already a string. There’s no method for converting a string to a string.
Thanks
Strange how it was working in IronPython though!
I’d be curious to see how you’re using it in IronPython. ToString()
is a specific method for converting another object type.
Both versions use str()
to stringify any object.
Exactly as the screen grab was.
I’m just checking someone’s got a plan view open before I let the script run (because the script is going to stick stuff onto that level) and giving them a warning if it’s not a plan view.
No inputs to the node.
I’ve deleted the ToString() now though and put it as a CPython3 node.
import clr
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
import System
# Get current Revit from RevitServices
doc = DocumentManager.Instance.CurrentDBDocument
# Get active view
activeView = doc.ActiveView
activeViewType = activeView.GetType()
activeViewTypeName = activeViewType.Name
if activeViewTypeName != "ViewPlan":
OUT = True, "" , activeViewTypeName
else:
OUT = False, "Sheets" , activeViewTypeName
Interesting. I mean it’s not doing anything, so you don’t need it anyway.
It’s important to note that ToString()
is from the Common Language Runtime (clr) and not an actual python method. The python method is str()
like I mentioned earlier, so that’s the method you should use regularly unless you know an object specifically uses the ToString()
method.
For normal Python I always use str() but for some of the weird Revit stuff ToString() seems needed… I’m still not sure exactly when though.
Probably because the Revit API is translated through CLR. Just depends on what you’re wanting to convert to a string.
Revit API objects are not necessarily Python native. Some of it wraps well, but not all. Those ‘not all’ bits need to be converted to Python native before they can use default Python content. That said it should be VERY rare that objects won’t convert to a string via the Python method str(obj)
, usually around enums, class instances which are being steered via the clr, and nested content.
I’ve been updating my package to CP3 lately and came across this also. For anyone who has the same issue in relation to enums, this is a workaround:
# Works in IP2.7
# OUT = ViewType.AreaPlan.ToString()
# CP3 workaround
OUT = System.Enum.GetName(ViewType, ViewType.AreaPlan)
for info, It’s solved in PythonNet3
with PythonNet3 Net objects are no longer automatically converted into Python objects as with PythonNet2