What's new

Custom Modules with AlibreScript

bolsover

Senior Member
I'm doing my level best to create some reusable scripts for a new project - but one particular issue is causing me real problem..

I want to break the project into a few modules and have a 'main' script that accepts user input and collects the values into an array and calls routines from the modules as needed.

The problem I'm facing is that I can't figure out how to invoke (some) AlibreScript classes methods from these separate modules.

I have the following 'main' script:
This part of test case does draw a circle on the XY plane using the public constructor circlea = Circle([1,1], 3, False)
The script then instantiates a new TestClass from the separate Python file (module) 'CalledModule'

Code:
from __future__ import division
sys.path.append(ScriptFolder)
import CalledModule
reload(CalledModule)
part = CurrentPart()
sketch = part.AddSketch("Test", part.GetPlane('XY-Plane'))
circlea = Circle([1,1], 3, False)
sketch.AddCircle(circlea)
testClass = CalledModule.TestClass(sketch)

Here is the code for the CalledModule file:
This code will draw circlea but fails with the call to circleb = Circle([0,0],5,False) with the message "NameError: global name 'Circle' is not defined"
I don't understand why this should fail in the CalledModule when the same code in the 'main' routine runs without problem.
I'm guessing I need some import statement - but I can't work out what that might be.

Code:
from __future__ import division

class TestClass:
 
  def __init__(self, sk):
    self.sketch = sk
    self.drawCircle()

  def drawCircle(self):
    circlea = self.sketch.AddCircle(1,1, 8, False)
    self.sketch.AddDimension(circlea)
    print circlea.Radius
    
    circleb = Circle([0,0],5,False)
    self.sketch.AddCircle(circleb)

Any help would be much appreciated.

DB
 

Attachments

  • CalledModule.py
    354 bytes · Views: 3
  • TestScript.py
    307 bytes · Views: 2

NateLiquidGravity

Alibre Super User
Does this work? I'm not at a PC to test.
Python:
from __future__ import division
sys.path.append(ScriptFolder)
from CalledModule import TestClass
part = CurrentPart()
sketch = part.AddSketch("Test", part.GetPlane('XY-Plane'))
circlea = Circle([1,1], 3, False)
sketch.AddCircle(circlea)
testClass = TestClass(sketch)
 

bolsover

Senior Member
Hi Nate
No.. the problem is in the CalledModule script.

I have just tried adding 'sys.path.append(ScriptFolder)' into the CalledModule script.

I get the following error:

Traceback (most recent call last):
File "<string>", line 6, in <module>
File "C:.....\Alibre Script Library\Bolsover\TestScripts\CalledModule.py", line 2, in <module>
NameError: name 'sys' is not defined
>>>

I'm thinking the problem is something quite fundamental about the way the Alibre scripting engine works. Quite possibly some sort of pas issue.

DB
 

bolsover

Senior Member
Problem solved!!:)

The Called module needs an import:

Code:
from AlibreScript.API import *

Didn't see that anywhere in the docs though.

DB
 
Top