canvasObjects; found PartInstance, expecting tuple in Abaqus/CAE - python

I'm developening an Abaqus/CAE plug-in, in this plug-in i'm using the gui toolkit, and i have a button that uses the PickStep, on click the button i can select a PartInstance in the viewport.
Then i want to export the selected PartInstance to an .obj file but when i try it, abaqus displays an error.
This is an example of my PICK BUTTON:
# PICK BUTTON 1
pickHf = FXHorizontalFrame(p=col2, opts=0, x=0, y=0, w=0, h=0, pl=0, pr=0, pt=0, pb=0, hs=DEFAULT_SPACING,
vs=DEFAULT_SPACING)
# Note: Set the selector to indicate that this widget should not be
# colored differently from its parent when the 'Color layout managers'
# button is checked in the RSG Dialog Builder dialog.
pickHf.setSelector(99)
label1 = FXLabel(p=pickHf, text='' + ' (None)', ic=None, opts=LAYOUT_CENTER_Y | JUSTIFY_LEFT)
pickHandler1 = DBPickHandler(form, form.uper, 'Select a 3D, discrete and dependent meshed instance', INSTANCES,
1, label1)
icon = afxGetIcon('select', AFX_ICON_SMALL)
FXButton(p=pickHf, text='\tPick Items in Viewport', ic=icon, tgt=pickHandler1, sel=AFXMode.ID_ACTIVATE,
opts=BUTTON_NORMAL | LAYOUT_CENTER_Y, x=0, y=0, w=0, h=0, pl=2, pr=2, pt=1, pb=1)
I save the value in an ObjectKeyword:
self.uper = AFXObjectKeyword(self.cmd, 'uper', True, pickedDefault)
This is how i export the PartInstance to .obj:
print 'Uper - ' + uper[0].name
f.write('Uper - '+uper[0].name+'\n')
session.writeOBJFile(fileName='C:/temp/Uper.obj', canvasObjects=(uper[0]))
That displays and error, and i also tried this:
print 'Fixed - ' + fixed[0].name
f.write(fixed[0].name+'\n')
fixedobj = open('Fixed.obj', 'w')
pickle.dump(fixed[0], fixedobj)
fixedobj.close()
But that does not work either.
I get this error:
canvasObjects;found PartInstance, expecting tuple

This answer will help you. On your call to session.writeOBJFile you are trying to create a one element tuple for the canvasObjects argument. Simply wrapping the item in parentheses won't achieve that. You need to add a comma to make it a tuple:
session.writeOBJFile(fileName='C:/temp/Uper.obj', canvasObjects=(uper[0],))
The Abaqus documentation says this about canvasObjects:
canvasObjects
A sequence of canvas objects to export.
I'm not sure if PartInstance is considered a canvas object or not, but you may still have problems even after correcting the argument to be a tuple. If so, make sure the items of the tuple are proper canvas objects.

Related

Maya Python GUI - Making a picker - how to be able to drag select buttons on a window using maya.cmds?

I'm trying to make a picker. As a test, I currently have 3 cubes in the scene, they represent the arm. I've stored the selection into the button's command, and individually it works. I could even store all 3 in one button selection, but I'd like to be able to drag select all 3 of them at once and make it more intuitive and flexible
I'm aware I could use PySide or QT for this, but I'm trying to keep it to maya.cmds for certain reasons. Basically without having to install additional modules and such to Maya.
Here's the code.
import maya.cmds as cmds
import maya.mel as mel
cmds.window("Arm_Control", title="Arm", wh=[288.0, 288.0], s=False)
cmds.formLayout("formLayout", numberOfDivisions=100, w=288.0, h=288.0)
#Creates the cubes and parents them to each other. This is supposed to mimick an arm
cmds.polyCube(n="Shoulder")
cmds.polyCube(n="Elbow")
cmds.polyCube(n="Wrist")
cmds.setAttr("Elbow.ty", -1.5)
cmds.setAttr("Wrist.ty", -3)
cmds.parent("Wrist", "Elbow", )
cmds.parent("Elbow", "Shoulder" )
#We create the buttons and a function that stores the selection sets. Upon pressing the button, we select the cubes.
def Wrist_Button_SS(button):
exec(str( cmds.select("Wrist")))
button_Wrist_Button_SS = Wrist_Button_SS
cmds.button("Wrist_Button_SS", l="Wrist", ann="", recomputeSize = True, bgc=[0.45400167849240863, 0.45400167849240863, 0.45400167849240863], h = 49.362586349587794, w = 100.0,c=button_Wrist_Button_SS, parent ="formLayout")
cmds.formLayout("formLayout", edit=True, attachForm=[("Wrist_Button_SS", "top",191.40357806156595), ("Wrist_Button_SS", "left",82.79126994609834)])
def Elbow_Button_SS(button):
exec(str( cmds.select("Elbow")))
button_Elbow_Button_SS = Elbow_Button_SS
cmds.button("Elbow_Button_SS", l="Elbow", ann="", recomputeSize = True, bgc=[0.45400167849240863, 0.45400167849240863, 0.45400167849240863], h = 49.362586349587794, w = 100.0,c=button_Elbow_Button_SS, parent ="formLayout")
cmds.formLayout("formLayout", edit=True, attachForm=[("Elbow_Button_SS", "top",119.03931848716638), ("Elbow_Button_SS", "left",81.88357425928116)])
def Shoulder_Button_SS(button):
exec(str( cmds.select("Shoulder")))
button_Shoulder_Button_SS = Shoulder_Button_SS
cmds.button("Shoulder_Button_SS", l="Shoulder", ann="", recomputeSize = True, bgc=[0.45400167849240863, 0.45400167849240863, 0.45400167849240863], h = 49.362586349587794, w = 100.0,c=button_Shoulder_Button_SS, parent ="formLayout")
cmds.formLayout("formLayout", edit=True, attachForm=[("Shoulder_Button_SS", "top",44.684211468350256), ("Shoulder_Button_SS", "left",81.88357201371167)])
cmds.showWindow("Arm_Control")
I'm aware that this is a longshot. If I can't drag/marquee select on the buttons or have that way to select selection sets, I might make it so that the user can hold shift when clicking the buttons to add onto the selection, but I'd prefer to set it up with a drag selection, sort of like DW picker or Animschool picker. Thank you to whoever can help out with this.

Python-docx how to set font spacing?

How to set font spacing in python-docx or how to add element to w:rPr?
//<w:rPr> <w:spacing w:val="200"/> </w:rPr>
There is no API support for this setting in python-docx.
Adding a <w:spacing> element will work if that's what Word does, however the sequence in which child elements appear is, in general, significant in WordprocessingML (the XML schema .docx files adhere to). If you don't get the w:spacing element in the right order among the w:rPr child elements or you add one when one is already there, you'll trigger a repair error.
So you need something like this:
def run_set_spacing(run, value: int):
"""Set the font spacing for `run` to `value` in twips.
A twip is a "twentieth of an imperial point", so 1/1440 in.
"""
def get_or_add_spacing(rPr):
# --- check if `w:spacing` child already exists ---
spacings = rPr.xpath("./w:spacing")
# --- return that if so ---
if spacings:
return spacings[0]
# --- otherwise create one ---
spacing = OxmlElement("w:spacing")
rPr.insert_element_before(
spacing,
*(
"w:w",
"w:kern",
"w:position",
"w:sz",
"w:szCs",
"w:highlight",
"w:u",
"w:effect",
"w:bdr",
"w:shd",
"w:fitText",
"w:vertAlign",
"w:rtl",
"w:cs",
"w:em",
"w:lang",
"w:eastAsianLayout",
"w:specVanish",
"w:oMath",
),
)
return spacing
rPr = run._r.get_or_add_rPr()
spacing = get_or_add_spacing(rPr)
spacing.set("val", str(value))
Then you would call this for each run that needs that setting like so:
run_set_spacing(run, 200)
I tried the answer by scanny, It did not work. But the output document XML <w:spacing val="200"> is close to the right format.
The right format is <w:spacing w:val="200">
Try this
from docx.oxml.ns import qn
spacing.set(qn('w:val'), str(value))

Setting background color of wordx

im trying to set a report default background color but it appears black instead of blue, can anyone help?
Why is that? do i need to use another format that is not hex? iv tryied with other format and still nothing.
docpath="/Users/ricardosimoes/Desktop/DESKTOP/Jira/my_word_file.docx"
mydoc = docx.Document()
section_h = mydoc.sections[0]
header = section_h.header
styles = mydoc.styles
style = styles.add_style('Tahoma',WD_STYLE_TYPE.PARAGRAPH)
style.font.name = 'Tahoma'
style.font.size = Pt(11)
shd = OxmlElement('w:background')
# Add attributes to the xml element
shd.set(qn('w:color'), '#0000FF')
shd.set(qn('w:themeColor'), 'text1')
shd.set(qn('w:themeTint'), 'F2')
# Add background element at the start of Document.xml using below
mydoc.element.insert(0, shd)
# Add displayBackgroundShape element to setting.xml
shd1 = OxmlElement('w:displayBackgroundShape')
mydoc.settings.element.insert(0, shd1)
paragraph_h = header.paragraphs[0]
runheader = paragraph_h.add_run()
runheader.add_picture("client_report/report_img/titulo.png", width=docx.shared.Inches(5), height=docx.shared.Inches(1))
mydoc.add_picture("client_report/report_img/bottom.png", width=docx.shared.Inches(5),
height=docx.shared.Inches(1))
mydoc.save(docpath)
The snippet seems to supply all three of the w:color, w:themeColor and w:themeTint attributes, and the latter two override the first one. The ECMA-376 standard says about the w:color attribute:
If the background specifies the use of a theme color via the themeColor attribute, this value is ignored. [Note: Applications are discouraged from specifying both the color and themeColor attributes on the same parent element. end note]
I haven't tested this, but removing themeColor and themeTint (and dropping the # from 0000FF) should cause the blue color to show up.

How can i use a function to condense my code?

fairly new to programming & i know i can use a function to condense this code bc its just mainly text im writing and ive used so many lines already. I've played around with defining my own function etc and i failed. Anyone know how i can make a function that includes like font size, if i want bold and font face???
This was my most recent attempt at doing it - but it didnt work:
def style(x, y, z,):
return Entry(Point(x,y),"z")
Below is my actual code.
# create the window to hold the contents
win = GraphWin("Text & Shapes Frenzy", 500, 500)
win.setBackground("Yellow")
#Setting the Title Screen:
title=Text(Point(400,250),"Text & Shapes Frenzy")
title.setFace('arial')
title.setSize(30)
title.setStyle('bold')
title.setFill('White')
title.draw(win)
#Mini-Heading
subheading=Text(Point(400,290), "Click to continue!")
subheading.setFace('courier')
subheading.setSize(22)
subheading.setFill('Blue')
subheading.draw(win)
message=Text(Point(400,50),"""Each click on the next page, adds a new
shape or text!""")
message.setFace('arial')
message.setSize(24)
message.setStyle('bold')
message.setFill('Red')
message.draw(win)
Use
def create_title(x,y,text,face,size,style,fill):
title=Text(Point(x,y),text)
title.setFace(face)
title.setSize(size)
title.setStyle(style)
title.setFill(fill)
title.draw(win)
return title
title=condense_funtion(400,250,"Text & Shapes Frenzy",.........)
subheading=condense_funtion(.........)
message=condense_funtion(.........)
replacing your values when calling the function cheers!
This is not the perfect solution but it is just to give you an idea. You can make a function that construct the object for you like this:
def set_obj(x=None, y=None, text=None, face=None, size=None, style=None, fill=None, background=None):
myobj = Text(Point(x,y), text)
if face:
myobj.setFace(face)
if size:
myobj.setSize(size)
...
return myobj
message = set_obj(x=400, y=500, text='bla bla', face='arial', size=24, style='bold', fill='Red', background=None)

Create Optionmenu with for loop

I am pretty new to Python as well as coding in general.
I've got 42 Labels (Strings) stored in a list.
My aim is to: for each of the 42 labels, create a Tkinter Label displaying the String and a Dropdown List (Tkinter Optionmenu).
Two problems occur:
I get an index error for the list of variables (for the optionmenus), here is the output of my Console:
varDd[i] = StringVar(root)
IndexError: list assignment index out of range
The Canvas is Scrollable as I intended, but the content doesn't scroll with it
My complete Code:
https://codepaste.net/6jkuza
The essential part:
def createDdList():
del labelsDd[:]
del listsDd[:]
if len(labels) > 1:
varDd = [len(labels)]
for i,label in enumerate(labels):
# Erstelle Labels mit den Markerlabels im scrollbaren Canvas
labelsDd.append(Label(contentCanvas,text=label))
labelsDd[i].pack()
# Erstelle die Dropdown Listen im scrollbaren Canvas
varDd[i] = StringVar(root)
listsDd.append(OptionMenu(canvas,varDd[i],*labels))
listsDd[i].place(x=70,y=10+(30*i))
contentCanvas = Frame(canvas,bg='#FFFFFF')
canvas.create_window((0,375),window=contentCanvas)
The first issue arrises from this like:
varDd = [len(labels)]
While this syntax is used in other languages to mean "a list with this many elements in python it is just a list with one number:
>>> labels = range(4)
>>> v = [len(labels)]
>>> v
[4]
Instead you probably want something along the lines of this:
>>> varDb = [None]*len(labels)
>>> varDb
[None, None, None, None]
Then you can index it the same way as your other lists
The other issue is that using .place() inside a canvas places it on the screen but not on the canvas space itself, to do that you need to call canvas.create_window so this line:
listsDd[i].place(x=70,y=10+(30*i))
would translate to this:
canvas.create_window(70, 10+(30*i), window = listsDd[i])

Categories

Resources