Importing geometry on a specific location using python

Hi,

I am new to VC but I need to make a simulation of a cartesian robot that I designed for my master thesis. I would need to import geometry (a metal plate where parts are cut out using a laser) so that the robot can grasp the objects and place them on pallets.

My question was: is there a way so that I can make a python script that asks a uri and opens the part on a specific location? If so, how?

Thanks for helping!

Kind regards,
Louis

Hello,
There’s an API method for importing a CAD geometry (of supported formats) in the given XYZ position:

app = getApplication()
vec = vcVector.new(1000, 0, 0) # Position X=1000
file_uri = ‘File uri here’
loadgeomcmd = app.findCommand(‘loadGeometryAsComponent’)
loadgeomcmd.execute(file_uri, vec)

The reference for this command is available in: https://help.visualcomponents.com/4.10/Premium/en/Python_API/vcCommand.htm

In addition to what KustiH said, you can also pre-position a piece of material in a specific location and then make him invisible, and when needed, use part.clone() or app.cloneComponent(part) to copy it, and of course you’ll need code similar to the following to remove the extra component:

a = part.clone()
temp.append(a)

def OnReset():
  for i in temp:
    try:
      i.delete()
    except:
      pass
  temp = []

Thanks Kusti!

Is there also a way to choose a folder and it imports all CAD-files from that folder? Because my code is the following:

from vcCommand import *
from vcHelpers.Output import *
import vcVector
import os.path

app = getApplication()
cmd = getCommand()
#output = Output()
setWarnings(False)
setErrors(False)
setInfo(True)

def OpenFile(prop):
open_dialog = app.findCommand(‘dialogOpen’)
open_dialog.OnPostExecute = read_file
open_dialog.Param_3 = ‘STL file (.stl) |.stl’
open_dialog.execute()

def first_state():
executeInActionPanel()

def read_file(open_dialog):
open_dialog.OnPostExecute = None
if not open_dialog.Param_2:
return
path = open_dialog.Param_1[8:]
info(“-------------PATH-------------”)
info(path)
info(“-------------------------------”)

vec = vcVector.new(0,4000,0)
loadgeomcmd = app.findCommand('loadGeometryAsComponent')
loadgeomcmd.execute(path, vec)

addState(first_state)

btnOpen = cmd.createProperty(VC_BUTTON, “OPEN FOLDER”)
btnOpen.OnChanged = OpenFile

txtInput = cmd.createProperty(VC_STRING, ‘URI’)

The problem is open_dialog does not support choosing a folder

Hmm. The Help documentation says the following about the file type filter argument in dialogOpen: “A filter argument defines file extension filter(s) using a standard Windows filter format.” (which is specified further on Microsoft’s page FileDialog.Filter Property (Microsoft.Win32) | Microsoft Learn). I don’t believe the file dialog even allows selecting a folder, or at least I have never seen the default file dialog do so.

I would advise you to implement the folder selection as follows:
User chooses one .stl file from the folder, after which the file path is split to path and filename using os.path.split(). After that, you could loop through the files in the folder with the following syntax:

folder_uri, filename = os.path.split(file_uri)
for root, dirs, files in os.walk(folder_uri):
  for file in files:
    # Checking for file extension here, and calling appropriate functions

BAD:s comment was a good addition; please remember that importing geometries during a simulation run has drawbacks. Invisible components are one option; creating a product type before simulation run and using a feeder component to spawn these products is one option. Best of luck for your project!