This code is a mix of my example AlibreX and AlibreScript IronPython code (I've shared here before) and my sensor/trigger code from VB.NET, simplified and converted to IronPython 2.7. This is only a demo and is not a ready-to-use solution. This is an advanced topic, you should understand the code, test it using test files not anything important. Paste the code into an AI to aid in your understanding.
That was my disclaimer...
I stopped using the AlibreScript addon after making Alibre Python shell. There are differences between my addon and the AlibreScript addon. The short story is related to module importing and preloading. AlibreScript addon is in a panel docked inside the window, my addon is in its own window. The addon is a generic Python shell without any preloaded Alibre APIs. You need to import modules and libraries manually. It does run inside the Alibre Design process so it can access libraries and modules the are loaded inside Alibre Design. I would need to update the addon to fully support this.
My addon does not stop or release objects after the window is closed. If you have a window that is modeless with non-blocking code or code running async it will continue to run even with the window closed. This is how my sensors work. I have code running in the background checking property values. To stop a sensor I have commands that set a Boolean value and clean up the objects.
To stop the example code just close the window. I made a quick video demo in case my writing is confusing. If you would rather use the AlibreScript addon the code should work with minor tweaks.
Code: AI added the comments
Python:
import sys
import clr
import time
from System import TimeSpan
from System.Threading import Thread, ThreadStart
# Update script paths to the new version
sys.path.append("C:\\Program Files\\Alibre Design 28.0.2.28126\\Program")
sys.path.append("C:\\Program Files\\Alibre Design 28.0.2.28126\\Program\\Addons\\AlibreScript")
clr.AddReference("AlibreX")
clr.AddReference("AlibreScriptAddOn")
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
import AlibreX
# Update PythonLib paths
sys.path.append("C:\\Program Files\\Alibre Design 28.0.2.28126\\Program\\Addons\\AlibreScript\\PythonLib")
sys.path.append("C:\\Program Files\\Alibre Design 28.0.2.28126\\Program\\Addons\\AlibreScript")
sys.path.append("C:\\Program Files\\Alibre Design 28.0.2.28126\\Program\\Addons\\AlibreScript\\PythonLib\\site-packages")
import AlibreScript
from AlibreScript.API import *
clr.AddReference('System.Runtime.InteropServices')
from System.Runtime.InteropServices import Marshal
# Automation hook and object handling
alibre = Marshal.GetActiveObject("AlibreX.AutomationHook")
root = alibre.Root
myPart = Part(root.TopmostSession)
session = root.Sessions.Item(0)
objADPartSession = session
# Print session and body information
print session.FilePath
print objADPartSession.Bodies.Count
b = objADPartSession.Bodies
verts = b.Item(0).Vertices
print verts.Count
# Print vertex points
def printpoint(x, y, z):
print "{0} , {1} , {2}".format(x, y, z)
for i in range(verts.Count):
vert = verts.Item(i)
point = vert.Point
printpoint(point.X, point.Y, point.Z)
# Configuration handling
ConfigurationStep = session
config_count = ConfigurationStep.Configurations.Count
ConfigurationStep.ActiveConfiguration = ConfigurationStep.Configurations.Item(0)
# Configuration switching function
def switch_configurations():
i = 0
while True:
item = ConfigurationStep.Configurations.Item(i % config_count)
ConfigurationStep.ActiveConfiguration = item
print "Switched to Configuration:", item.Name
time.sleep(5) # Switch every 5 seconds
i += 1
# Start a .NET thread for non-blocking configuration switching
thread = Thread(ThreadStart(switch_configurations))
thread.IsBackground = True # Ensure the thread exits with the script
thread.Start()
# Windows dialog handling
Win = Windows()
Win.InfoDialog('This code is from Alibre Script.', myPart.FileName)
Win.ErrorDialog('This code is from Alibre Script!', myPart.LastAuthor)
print Win.QuestionDialog('This code is from Alibre Script?', myPart.CreatedBy)
# Keep the script alive to allow the background thread to run
while True:
time.sleep(1) # Prevent the script from exiting immediately
Demo and uninstalling Revit:
I forgot to mention the example is not interactive. You cannot edit features or use any commands that use transactions or touch features, configurations, parameters, etc. I have logic in my project that checks the state and only runs code when AD is idle. If you try to create or edit a feature AD will crash. You need to handle this in your code if you want an interactive tool.
{edit}
After thinking about this, it is possible to edit and create features while the code is running. But It's dangerous without something handling this to do it safely. So, I don't recommend it.