If you wait long enough, we’ll get with the next update access to the new Process features, which handles Products.
But you can still do it with the old methods/functions.
Here an example I made today:
import io
import csv
import random
from vcScript import *
def get_root():
"""
Return the RootFeature of the current component.
"""
return getComponent().RootFeature
def get_random_material():
"""
Return a random material
"""
return random.choice(
getApplication().Materials
)
def get_block(name):
"""
Get a feature block by name
"""
return getComponent().findFeature(name)
def create_block(name):
"""
Create a new block with the given name and return the block
"""
root = get_root()
return root.createFeature(VC_BLOCK, name)
def edit_block(name, height, length, width, material=None):
"""
Edit the block. If the block does not exits, it will be
created and then the properties are set.
"""
root = get_root()
if material is None:
material = get_random_material()
block = get_block(name)
if not block:
block = create_block(name)
block.Height = str(height)
block.Length = str(length)
block.Width = str(width)
block.Material = material
block.rebuild()
def create_properties():
"""
Create the missing properties in the component.
"""
comp = getComponent()
size_props = ("Height", "Length", "Width")
for prop_name in size_props:
prop = comp.getProperty(prop_name)
if not prop:
comp.createProperty(VC_REAL, prop_name)
material_list = comp.getProperty("ProductMaterial")
if not material_list:
material_list = getApplication().Materials
# Usually VC_ constants are used for the first argument
# In this case it must be a str. "Ref<Material>"
# is a List with all available Materials
comp.createProperty("Ref<Material>", "ProductMaterial")
csv_file_uri = comp.getProperty("CSV Data")
if not csv_file_uri:
comp.createProperty(VC_URI, "CSV Data")
def get_sizes():
"""
Returns Height, Length and Width
from properties
"""
comp = getComponent()
h = comp.getProperty("Height").Value
l = comp.getProperty("Length").Value
w = comp.getProperty("Width").Value
return h, l, w
def get_selected_material():
"""
Return material from DropDownList of Materials
"""
material = getComponent().getProperty("ProductMaterial")
if not material or not material.Value:
return get_random_material()
else:
return material.Value
def from_csv():
"""
Read Data from a custom csv file
A URI Property is used, to select the csv file to process
Return a random row from the csv file
where the fields are converted to floats
Format:
height, length, width
"""
uri = getComponent().getProperty("CSV Data").Value
path = uri.replace("file:///", "")
with io.open(path, "rt", encoding="utf8") as fd:
reader = csv.reader(fd)
row = random.choice(list(reader))
return tuple(map(float, row))
def OnSignal(signal):
pass
def OnRun():
# Create missing properties
create_properties()
# Example to change the Feature Block during runtime
while True:
try:
# reading from csv and converting can
# throw an exception
h, w, l = from_csv()
mat = None
except Exception as e:
print(e)
# got no data from csv
# getting data from the properties
h, l, w = get_sizes()
mat = get_selected_material()
# set the propierties
edit_block("simple block", h, l, w, mat)
delay(2)
Example data of csv:
666,42,55
42,42,66
42,42,616
1000,1000,1000
90,90,90
500,500,500
The code is in the component itself. It should be changed to fit to your requirements.
For example you may not need the attached properties to the Component.
Or you can extend the csv function to get a name, material or other settings.