What's new

Some basic questions...

DavidJ

Administrator
Staff member
Investigating a need to set property value for a design, I initially could only set those that appear in the 'General' window.

From Scripts posted by IDSLK (Stefan), I saw that it is possible to set the 'Part Data' properties - though I was struggling to follow the scripts.

I've taken Stefan's latest script - thanks Stefan ! - and stripped out all but the most basic functionality. This is to help me to understand how things work, so that I can move on to creating my own automations. There are a few things that I don't understand - I'm not sure to what extent some of this may be that the Python use might differ slightly from that in the books I'm studying, which items might be specific to AlibreScript, and which are just stylistic.

My 'hacked' code is attached

Code:
# initial stuff is about importing the capability to connect to extended properties

import clr
clr.AddReferenceToFileAndPath(r'C:\Program Files\Alibre Design\Program\AlibreX.dll')
from AlibreX import ADExtendedDesignProperty
 
# I'm not clear quite what this bit does - but it is used below
Root = Global.Root
ThisSession = Root.TopmostSession

#main--------------------
#This next line actually sets the 'extended' property value

ThisSession.DesignProperties.ExtendedDesignProperty(ADExtendedDesignProperty.AD_VENDOR,'Toblerone')

# this example alters Vendor but you can substitute in the above line from any of the full extended property names
# AD_COMMENT
# AD_COST_CENTER
# AD_CREATED_BY
# AD_CREATING_APPLICATION
# AD_CREATED_DATE
# AD_DOCUMENT_NUMBER
# AD_ENG_APPROVAL_DATE
# AD_ENG_APPROVED_BY
# AD_ESTIMATED_COST
# AD_KEYWORDS
# AD_LAST_AUTHOR
# AD_LAST_UPDATE_DATE
# AD_MATERIAL
# AD_MFG_APPROVED_BY
# AD_MFG_APPROVED_DATE
# AD_PRODUCT
# AD_RECEIVED_FROM
# AD_REVISION
# AD_STOCK_SIZE
# AD_SUPPLIER
# AD_TITLE
# AD_VENDOR
# AD_WEBLINK

Questions to anyone who can help...

1. What is clr ?, and what does the r prior to the path name mean ? ( root maybe ?)

2. Root, Global.Root , TopmostSession - what do these relate to? How does this map to CurrentPart, some other part, etc. in Alibre ? I can use this code to set a value for the file it is in, but I know you can also run code in one AD Part that (say) modifies another Part. I don't see how this ties together .

3. I can see (by trying it) that I can't simply replace 'ThisSession' in the main functional line with CurrentPart - it doesn't work. This is probably just an extension of my lack of understanding at 2.

As you can tell I'm new to Python - though I have coded in other languages previously.
 

idslk

Alibre Super User
Hello David,
  1. regardless of an possible internetsearch... Common Language Runtime
    the 'r' is a prefix for 'rawstring'. Needed to use the windows format for the file. (the unix/linux uses / instead of \)
  2. As far as you need an API function to get access to the ExtendedDesignProperties, you have to "describe" which data have to be altered...
    This has to have an format for the API. For more info, take a look at the api help file.
  3. see 2)
One tip: if you are learning/experimenting with that and your not sure about, it makes sense to use a system with data, which is not the same you are using for work...;)

Regards
Stefan
 

DavidJ

Administrator
Staff member
Thanks Stefan, but item 2 you haven't answered my question - you answered something else. Maybe I didn't explain the question clearly enough.
 

idslk

Alibre Super User
Hello David,

here is a sniplet from the API Help:
(------------
IADRoot:
Once we have the AutomationHook, next task is to get the root object for the Alibre features tree. These features include Assemblies, Part, Part Features, various sessions and various such Alibre objects, which you will find when you run Alibre.
------------)

A thing like CurrentPart() is a function provided via AlibreScript and not from the API directly, so it has an other syntax.
Take a good cup of tea, sit down, relax and read the API Help to get a picture of the structure you want to access.
The help is indeed not he best, but more than nothing.
To get a grip on the mix of API and AlibreScript use a standard python package and try to reach things from outer alibre...
Explaining the whole stuff here, will be over the limits of a post...

But more or less, if you have specific questions after reading the API Help, i will try to answer them.:)

Regards
Stefan
 

DavidJ

Administrator
Staff member
API Help - not at all easy to follow, assumes a lot of prior knowledge. Written for Developers, not for novices.

I presume that Root = Global.Root is the equivalent of using IADRoot: in the API ? I'm not sure where the Global. comes from - doesn't seem to be Python, is it from API, CLR.....? I'm guessing that this Global has a meaning similar to Global variable scope?


ThisSession = Root.TopmostSession - is this 'picking up' the current Alibre session? I'm assuming the current session sits 'Topmost'.


It isn't at all clear to me how 'sessions' are managed in the 'hierarchy' that gets mentioned.

Presumably if I wanted to change the properties of a new part I'll need to create a NewPart (easy from script) then somehow find its corresponding session, and (for example) set
NewPartSession = (however I find the session).​
Then I can modify the line that actually sets the property by replacing 'ThisSession' with 'NewPartSession'

OR once I create a new part, does its session become Topmost ?
 

idslk

Alibre Super User
Hello David,
assumes a lot of prior knowledge. Written for Developers, not for novices.
regardless this, may the following gives you a hook:
Code:
#provide access to the AlibreX.dll
import clr
clr.AddReferenceToFileAndPath(r'C:\Program Files\Alibre Design\Program\AlibreX.dll')

#create a new part with AlibreScript
Part('David')

#Setup access to the active, topmost Session !New created Session will be the topmost one!
Root = Global.Root
ThisSession = Root.TopmostSession

#This is only to show session information
print 'Count of open Sessions:',Root.Sessions.Count
for i in range(Root.Sessions.Count):
  print 'Number and Name of the open Sessions: ',i, Root.Sessions.Item(i).Name

#only to show the session which will be affected from changes to 'ThisSession'
print 'Active (Topmost) Session: ',ThisSession.Name

if you go further, you should be able to create a new part without a using template, but with filled properties...;)

Please tell if it has helped you.
Thanks in advance
Stefan
 
Last edited:

DavidJ

Administrator
Staff member
Stefan, thanks - just before seeing you latest post, I just tried adding a line to create a new part. I found the rest of the code still worked. As you say the newest session must be Topmost.

Your line to create the new part was even simpler than mine - so I've learned more. Thanks again

I'll try that script out.
 

idslk

Alibre Super User
Hello David,
nice to hear this.
if you go further, you should be able to create a new part without a using template, but with filled properties...
and here for "all"
Demo with Vendor Property
NewPart will be created, Vendor will be set, You will be asked for a Folder to save in, NewPart will be saved there.
Code:
#provide access to the AlibreX.dll
import clr
clr.AddReferenceToFileAndPath(r'C:\Program Files\Alibre Design\Program\AlibreX.dll')
from AlibreX import ADExtendedDesignProperty

#create a new part with AlibreScript
NewPart = Part('IDSLK')

#Setup access to the active, topmost Session !New created Session will be the topmost one!
Root = Global.Root
ThisSession = Root.TopmostSession

#This is only to show session information
print 'Count of open Sessions:',Root.Sessions.Count
for i in range(Root.Sessions.Count):
  print 'Number and Name of the open Sessions: ',i, Root.Sessions.Item(i).Name

#only to show the session which will be affected from changes to 'ThisSession'
print 'Active (Topmost) Session: ',ThisSession.Name

ThisSession.DesignProperties.ExtendedDesignProperty(ADExtendedDesignProperty.AD_VENDOR,'IDSLK')

win = Windows()
folder = win.SelectFolderDialog('IDSLK SAVE FOLDER','Choose Folder for your new created part.')
#if you want to choose a folder use the two lines above
#if you want to use a fixed folder use the line below to define your folder
#folder = 'C:\Users\YourUserName\Desktop'

NewPart.Save(folder)

Regards
Stefan
 

DavidJ

Administrator
Staff member
Beginning to get there - had an example working that created a new part, saved it and closed it. But after I tried to get clever and do it with interface for new hidden, I've broken something and it won't work at all.

Odd - found a couple of things that 'get in the way' - creating the new part with Editor Hidden , part is created but the Property isn't set.

Closing the console Window in the part file I'm running he script from stops anything from working - it's obviously more than juts a display.
 
Last edited:

idslk

Alibre Super User
Hello David,
can you post some screenshots and your code?
And: What do you want to create at the end?
Regards
Stefan
 

DavidJ

Administrator
Staff member
Stefan, I have my Script achieving what I need for now - I'm presuming that hiding the Editor either 'hides' the session, or changes its 'position' so it isn't 'Topmost' - so the command to set the Property doesn't work.

I was a bit more surprised that turning off the Console would prevent things working - if not just a display option why have the opportunity to close the console?
 

DavidJ

Administrator
Staff member
Stefan - your script that lists sessions helped me to work out what happens with Hidden Editor parts - the session is listed, but isn't 'Topmost' (Active). I'll have to look at referring to the session in some other way if I want to manipulate a part for which the editor is hidden
 

idslk

Alibre Super User
Hello David,
might be the time for another cup of tea...;)
I assume that you are talking about the complete window if you're saying "the hidden editor"?
If you see your object of desire in the list of sessions, why don't try to say such as MyObjectOfDesire = Root.Sessions.Item(i) , where "i" should be the name or index of the session you want to manipulate. This looks a littel like ThisSession = Root.TopmostSession. I hope this helps. Regardless of the danger i'm repeating myself, what do you want to do (i'm a little bit curious...)?
Regards
Stefan
 

DavidJ

Administrator
Staff member
Stefan, as you may have guessed I was initially attempting to help Lew with his requirement to set Part Data properties (something that can't currently be done directly in Script). I'm new to Alibre Script, and initially I couldn't get anywhere much. Your earlier script to set Part Data from a spreadsheet gave me some ideas, but I was struggling to work it out. Your more recent script just setting Vendor had less in it and I was able to strip that down to bare essentials and get it to work - but still wasn't totally clear on everything (hence this thread). You've subsequently answered most of my questions on that.

Along the way various questions came up which were more general about use of Alibre Script - which I think I've now more or less understood.

By 'Hidden Editor' - I mean that I'd used Part('NewPart', True,True) as I wanted to try repeating what I'd already done, but without having the part session visible. I thought that might speed things up a little. If I'd not had to use the API, I think this would have been straightforward as I wouldn't have to worry about Sessions - I could have just addressed the NewPart directly from Script.

I don't have any specific target to achieve just at the moment - but having been forced to look at Script to try to answer a support ticket, I then wanted to understand it a bit more. In the particular case I was looking at, things are complicated enormously by the need to use the API, and the fact that (at least to me) how API is 'translated' into the structure for a Script command isn't very obvious.

All my previous programming has been either from before the days of object oriented programming (ALGOL, BASIC, FORTRAN), or has been in control systems for process plant (where things are designed to be much clearer to the human), so the form of things in Python and the API is unfamiliar to me.
 

NateLiquidGravity

Alibre Super User
Here is a undocumented trick I learned recently:
Code:
NewPart = Part('NewPart', True,True) # creates a new part hidden in the background using Alibre Script.
NewPartAD = NewPart._Part # gets Alibre Script to pass the API object for NewPart.
 

DavidJ

Administrator
Staff member
Nate, thanks - That second line looks just like what I needed and is simpler than all those references to sessions. I'll have to work out what it allows me to do and if I still need to use Stefan's session approach at any point.

My biggest problems remain

1. Finding stuff that is in the documentation (I'm probably not searching the Reference in the best way)
2. Working out how to tackle stuff that is buried down at API level

Still have gone from avoiding Script to having some capability in a few days....
 

DavidJ

Administrator
Staff member
Thanks again Nate - I got the ._Part thing to work, and now that list of available things working too (some there I can't even imagine what they might do!).

I still find the dot notation non-intuitive , it isn't always obvious to me what things need to be 'dotted along' to complete what I need to do - I don't find the API reference helps much with that. I'm sure a lot is down to familiarity - where I can find an existing example in Script it becomes a lot easier, even if I don't follow it 100%.
 

NateLiquidGravity

Alibre Super User

DavidJ

Administrator
Staff member
Yeah - it isn't too bad when working natively with Script, becomes much more 'interesting' when working out all that dot stuff for API stuff accessed from Script.

These tips are appreciated - they are not at all obvious to the novice, they're probably second nature to an experienced Developer.
 
Top