What's new

CurrentPart() not working

Martinm

Member
Am I missing something...?

>>>p=CurrentPart()

Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'CurrentPart' is not defined

Also the API reference has no entry for this function.
What I wanted to achieve is have the script affect the part that is being displayed in the same window. Calling
MyPart = Part('My Part')
causes a different window to pop up, which I find quite unconvenient.

- Martin
 
This is not exactly what you are looking for but you can try this:

In this code you need to have the file opened then type in the file name. While it isnt what you were looking for it is convenient in some ways. For instance you can have an assembly open and choose any component withing that assembly.

Code:
#--CURRENT FILE DIALOG--#
Win = Windows()
Options1 = []
Options1.append(['File name to modify ', WindowsInputTypes.String, '<File name>'])
Options1.append(['Note: ', WindowsInputTypes.Label, 'Please enter exact filename' + '\n' + 'Not case sensitive.'])

Values = Win.OptionsDialog('Current File?', Options1)
if Values == None:
    sys.exit(0)
File_name = Values[0]

    #-- ERROR DIALOG --#
try:
    P = Part(File_name, False)
except:
    Win.ErrorDialog("No file with that name found. Please open " + 'file and try again.', 'Error') + sys.exit(0)

This script creates a dialog box where you type in the file to get access to it. It has a lot of extra bits so you can also try the script below. This script is bare bones but will allow you to see what is actually giving you the required results.

Code:
print 'Enter file name to access. File must be open:'
File_name = str(Read())

P = Part(File_name, False) #This gets access to the file using the previous user input. File must be open.
#From now on use 'P' to represent the current part.

#For instance the code below will change the drescription of the current part:
print 'Enter description for file: ' + File_name
New_desc = str(Read())
P.Description = New_desc #Use 'P' to represent the part followed by '.[insert_attribute]'
print 'Description Changed. Have a nice day.'
 

Martinm

Member
CurrentPart only exists in scripts, not the console.
Andy

Thanks for the info - while I'm not a Python guru, I thought one of the key points of this language would be that you can try out everything from the command prompt. "Works only in scripts" is quite un-pythonic...

- Martin
 

Martinm

Member
OK, so now I am creating a new part (a drill bit rack, just a block with many holes).
How can I have the same clean start every time I run the script? If I just run it multiple times (while adding stuff to it), generated geometry adds up in the part. I tried part.Close() and then reload the part, but Close() closes the whole window, script and all.
So how can I reload a part - or how do I part.DeleteAll()?

- Martin
 

idslk

Alibre Super User
Hello Martin,

you can remove things which you have added with a script.
open a part, start alibre script, copy code and press play...
Code:
#Remove script driven design...
# get access to windows functionality
Win = Windows()
# Stay in current part window
CP = CurrentPart()
#Create Sketch
Cylinder_Sketch = CP.AddSketch('Cylinder_Sketch', CP.XYPlane)
#AddCircle on new sketch
Cylinder_Sketch.AddCircle(0, 0, 50, False)
# extrude into a cylinder
Cylinder = CP.AddExtrudeBoss('Cylinder', Cylinder_Sketch, 100, False)
#Info and wait for user
Win.InfoDialog('...and now cleanup','Demo')
#First remove Feature
CP.RemoveFeature(Cylinder)
#then remove sketch
CP.RemoveSketch(Cylinder_Sketch)
it won't touch manual drawn items...

Regards
Stefan
 

Martinm

Member
Hello Stefan,

OK, this works... but almost doubles the coding for the remove section.
I'd vote for part.Reload()...

- Martin
 

idslk

Alibre Super User
Hello Martin,
can you describe what part.Reload() should do? Maybe i will vote for this too ;-)
Another method do avoid things adding up is to check if they are already there - ok, it also more lines to code...
It also detects if your script is running on an unknown part if this part is using your choosen names for other geometry.
Code:
#Don't create Doubles ;-)...
Win = Windows()
CP = CurrentPart()

try:
 CP.GetSketch('Sketch<1>')
 print 'Already used!'
except:
 print 'free'

try:
 Cylinder_Sketch = CP.GetSketch('Cylinder_Sketch')
except:
 Cylinder_Sketch = CP.AddSketch('Cylinder_Sketch', CP.XYPlane)
 Cylinder_Sketch.AddCircle(0, 0, 50, False)

try:
 Cylinder = CP.AddExtrudeBoss('Cylinder',Cylinder_Sketch, 100, False)
except:
 print 'Feature already available'

Win.InfoDialog('...and now play it again Sam...','Demo')

Regards
Stefan
 

Martinm

Member
Well, it's a usability thing. The core proble is that I'd like to write the code iteratively, as this API is new for me and I need to see what the commands do (in addition to the completely random behavior of "direction" flags). So what I do is:
write some code
run it
change one command
run it again
next line
run it again

You get the point. The problem is how do I get back to a clean starting point without manually (or by additional code, as you suggested) having to remove whatever was generated before. So for a design "mypart" one could imagine
p.Close()
p = Part("mypart")
which would reload the part from a previously stored file. If you would like to run your script on a manually created geometry, you'd save that first and then run the code from that starting point. In case of my drill rack, I designed the base manually and just added the 91 holes with the script.
Unfortunately p.Close() shuts down AD entirely, so this is no good.
A suggestion would be to modify the part.Close() behavior or add a part.Reload() which would not have an intermediate empty screen between close and open.
Is there a "feature request" list? Probably we'll have to look at 2018.2 first, judging from the release notes a lot of work went into the script module.

- Martin
 

ajayre

Alibre Super User
I suggest your script creates a new part, creates the geometry you want, waits for a keypress then closes the new part. Now you are back to square one and can run it again.

Andy
 
Top