What's new

Add3DSketch throws COMException with CurrentPart()

stepalibre

Alibre Super User
FYI,
While debugging the GPT, I noticed issues around Part() vs CurrentPart(). The solution for now is to not use CurrentPart() in the GPT or manually fix it. This creates other issues, being that another window is opened with the new Part(). This is very cumbersome not to mention OpenAI services and the GPT beta being very slow lately.

From the error I guess, the call to AlibreScript.API.Part.Add3DSketch(String Name) is failing under CurrentPart()?

CurrentPart() is missing from the examples so maybe it's not the same as Part(). Creating a new part file for a simple sketch or other simple test I'm doing is wrong. I need the same session in-order to troubleshoot the GPT and scripting in general.
ChatGPT generated code:
Failed
Python:
import math
from AlibreScript import *
part = CurrentPart()
sketch3D = part.Add3DSketch('My3DSketch')
if sketch3D is None:
    raise Exception("Failed to create 3D sketch.")
sketch3D.StartEditing()
# Define start and end points of the line
startPoint = [0, 0, 0]  # Replace x1, y1, z1 with your start point coordinates
endPoint = [10, 10, 10]    # Replace x2, y2, z2 with your end point coordinates
# Add the line to the sketch
sketch3D.AddLine(startPoint, endPoint)
sketch3D.StopEditing()
print("Line added to 3D sketch successfully.")
>>>
Traceback (most recent call last):
  File "<string>", line 4, in <module>
EnvironmentError: System.Runtime.InteropServices.COMException (0x8000FFFF): Object reference not set to an instance of an object.
   at com.alibre.automation.ExceptionMap.handleException(Exception inputException)
   at com.alibre.automation.Alibre3DSketches.Add3DSketch(String name)
   at AlibreScript.API.Part.Add3DSketch(String Name)
   at Microsoft.Scripting.Interpreter.FuncCallInstruction`3.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
   at Microsoft.Scripting.Interpreter.DynamicInstruction`4.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
   at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
   at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
   at AlibreScript.UI.IronTextBoxControl.#9Gb(Object #FI)
>>>
1700671484792.png
Works
Python:
import math
from AlibreScript import *
part = Part('a_new_sk')
sketch3D = part.Add3DSketch('My3DSketch')
if sketch3D is None:
    raise Exception("Failed to create 3D sketch.")
sketch3D.StartEditing()
# Define start and end points of the line
startPoint = [0, 0, 0]  # Replace x1, y1, z1 with your start point coordinates
endPoint = [10, 10, 10]    # Replace x2, y2, z2 with your end point coordinates
# Add the line to the sketch
sketch3D.AddLine(startPoint, endPoint)
sketch3D.StopEditing()
print("Line added to 3D sketch successfully.")
>>>
Line added to 3D sketch successfully.
>>>
APIs
Type->AlibreScript.API.Part
Method->Add3DSketch(String Name)
Type->AlibreScript.API.Sketch3D
Method->StartEditing()
Method->StopEditing()
Method->AddLine(List StartPoint, List EndPoint)
Method->AddPoint(Double X, Double Y, Double Z)

1700669670015.png
 
Last edited:

stepalibre

Alibre Super User
I have solutions:


CurrentPart() should be called AlibreScriptSession() or similar.
 

NateLiquidGravity

Alibre Super User
If you put your code in the post please wrap it in code tags.

There are inline code tags using the >_ icon in the editor or type them yourself [ICODE][/ICODE]
And code block tags using the </> icon in the editor or type them yourself
Python:
[CODE=python]
[/CODE]

Note that for obvious reasons the first closing code tag was consumed by the display formatter instead of the second but you get the idea. Also you can select other languages for formatting code blocks.
 

stepalibre

Alibre Super User
The solution:

Python:
import math
from AlibreScript import *
part = Part(CurrentPart()._Part)
sketch3D = part.Add3DSketch('My3DSketch')
if sketch3D is None:
  raise Exception("Failed to create 3D sketch.")
  sketch3D.StartEditing()
# Define start and end points of the line
startPoint = [0, 0, 0] # Replace x1, y1, z1 with your start point coordinates
endPoint = [10, 10, 10] # Replace x2, y2, z2 with your end point coordinates
# Add the line to the sketch
sketch3D.AddLine(startPoint, endPoint)
sketch3D.StopEditing()
print("Line added to 3D sketch successfully.")

So in AlibreScript:

Part is really PartFeatures and other part level stuff

CurrentPart() is a reference to your topmost/active file by name/id

CurrentPart()._Part is the design session

Thanks @axeme and community!!!:):)
 
Last edited:

NateLiquidGravity

Alibre Super User
Yes, there is a distinct difference between the AlibreScript (API) and AlibreX API.
There are ways hidden in AlibreScript to transfer things back and forth but you can not access methods and properties from both in the same object (unless they happen to be named exactly the same but even then their use and results would be different)

There should be no reason that Part() and CurrentPart() are different though. I will have to test on my own machine and write back on that.
 

stepalibre

Alibre Super User
I will have to test on my own machine and write back on that.
That would help.

So far, this issue has only happened with Add3DSketch. I searched for Add3DSketch and the newest post is 2019. It has happened before but I don't remember what the code was doing.

Keep in mind this is for the GPT, I'll handle this in the data and instructions. We should document this issue but It's a low priority.
 
Last edited:

stepalibre

Alibre Super User
I tried it again and now it works. This is strange because the other methods were working fine and CurrentPart() was not. Now it is. There must have been an issue that resolved itself after the restart.
 

NateLiquidGravity

Alibre Super User
I tested all 3 codes you posted in this thread by copy/paste each into their own AlibreScript "New Script" window directly and all 3 worked without problems. Further running directly in AlibreScript the line from AlibreScript import * is unnecessary. Are you including it because you are running the code using your own ironpython addon implementation you talked about elsewhere?
 

stepalibre

Alibre Super User
I restarted my PC and tried it again and CurrentPart is working again. The other 2 methods worked just fine it was CurrentPart that was crashing. Thanks for checking.

Yes importing AlibreScript is for VS Code.
 

stepalibre

Alibre Super User
I experienced this issue again and I think it's caused by having multiple Alibre Design windows open with the Alibre Script addon enabled. When switching between windows CurrentPart() can fail. To solve this you can use other methods to get the current session when using multiple Alibre Script addon instances.
I restarted my PC
I'm on a Mac using Boot Camp and switched to Mac from PC.
 
Top