How to move newly created frames to a target position?

Hello,

I have a tray with 20 + positions that needs to have containers for each position.

I created some of the root frames (4 root frames, for 4 diffenrent parts that need to pleaced in the frames) by hand and moved them to the right position.

I created the other 20 + frames by script, but now I want to move them to their respective traget positions. I know the offsets from the root frames. So what I want to to is

  1. get the position of the respective root frame
  2. move the newly created frame to the root frame position
  3. move the newly created frame by the known offset to their target position

How can I achieve this in the script?
The script (creation of frames+moving) is only run once upon initialization.

This is what I have so far:

from vcScript import *
from collections import namedtuple
import sys
from vcHelpers.Output import *

_schlitten = "S1"
_containers = ["ContainerSechskant1","ContainerSechskant2","ContainerSechskant3","ContainerSechskantLöt"] 
_frames = ["FrameS1AufnahmeSechskant1","FrameS1AufnahmeSechskant2","FrameS1AufnahmeSechskant3","FrameS1Lötrohraufnahme"]

def OnSignal( signal ):
  pass

def OnRun():
  pass

def OnStart():


  targetNode = _comp.findNode("201_Y-Achse")
  for container in _containers:
    for number in range(1,5):
      getOrCreateBehaviour(_comp, targetNode, VC_COMPONENTCONTAINER, _schlitten + container + str(number))
  
  print("creating frames")
  targetFeature = _comp.findFeature("Aufnahmeschlitten-1")
  if targetFeature is not None:
    for frame in _frames:
      for number in range(1,5):      
        getOrCreateFeature(_comp, targetFeature, VC_FRAME, frame + str(number))
        rootFrameName = "Root" + frame
        rootFrame = _comp.findFeature(rootFrameName)
        # get root frame position        
        # move frame to target position

  
def getOrCreateProperty(comp, type, name, quantityName = None, defaultValue = None):
  prop = comp.getProperty(name)
  if not prop:
    prop = comp.createProperty(type, name)
    if quantityName:
      prop.Quantity = _app.findQuantity(quantityName)
    if defaultValue:
      prop.Value = defaultValue
  return prop

def getOrCreateBehaviour(rootNode, partentNode, type, name, automaticReset = True):
  beh = rootNode.findBehaviour(name)
  if not beh:
    beh = partentNode.createBehaviour(type, name)
  if type in [VC_BOOLEANSIGNAL, VC_REALSIGNAL, VC_INTEGERSIGNAL, VC_STRINGSIGNAL]:
    beh.AutomaticReset = automaticReset
  return beh

def getOrCreateFeature(rootFeature, parentFeature, type, name):
  print("in create feature " + name)
  feat = rootFeature.findFeature(name)
  if not feat:
    feat = parentFeature.createFeature(type, name)
    print("created feature")
  else:
    print("found featrue")
  return feat


### GLOBAL VARIABLES ###############################################
_comp = getComponent()
_app  = getApplication()
#_buildButton = getOrCreateProperty(_comp, VC_BUTTON, "Build Comp")
#_buildButton.OnChanged = onBuildButtonAction

Thanks for your help in advance
Thomas

Hi @thomas.klengel

vcFeature has PositionMatrix proeprty where you can write a vcMatrix object.
The API documentation for vcMatrix has methods like translateAbs() to move the matrix to desired offset.

I made a quick example. Hope it helps.

from vcScript import *
import vcMatrix as mat


comp = getComponent()
root_frame = comp.findFeature("Base")
root = comp.RootFeature
new_frame = root.createFeature(VC_FRAME, "My new frame1")
posmat = root_frame.PositionMatrix
posmat.translateAbs(100,200,300)
new_frame.PositionMatrix = posmat
comp.rebuild()
1 Like

Hi @Este
That worked like a charm. Thank you so much. :slight_smile:

Here is the complete result for the script:

from vcScript import *
import vcMatrix as mat

def createFramePattern(rootFeature, referenceFrameName, targetFrameName, dX, dY, dZ, posX, posY, posZ=1):
  '''
  Creates new Frames in a pattern relative to a reference frame

  'rootFeature"         : The feature to which to add the new frames
  'referenceFrameName'  : The name of the frame to which the new frames are offset
  'targetFrameName'     : The root name of new frame (the x,y and z position is added in the result)
  'dx'                  : The offset distance from the root frame in X direction
  'dx'                  : The offset distance from the root frame in Y direction
  'dx'                  : The offset distance from the root frame in Z direction
  'posX'                : The amount of positions in X direction
  'posY'                : The amount of positions in X direction
  'posZ'                : The amount of positions in X direction
  '''
  targetFeature = _comp.findFeature(rootFeature)
  if targetFeature is not None:
      rootFrame = _comp.findFeature(referenceFrameName)    
      for zPos in range (0,posZ):
        for yPos in range (0,posY):
          for xPos in range (0,posX):
            frameName = targetFrameName
            if posX>1:
              frameName=frameName +"X"+ str(xPos+1)
            if posY>1:      
              frameName=frameName +"Y"+ str(yPos+1)
            if posZ>1:      
              frameName=frameName +"Z"+ str(zPos+1)

            new_frame = targetFeature.createFeature( VC_FRAME, frameName)
            posmat = rootFrame.PositionMatrix
            posmat.translateAbs(dX*xPos,dY*yPos,dZ*zPos)
            new_frame.PositionMatrix = posmat

_rootFeature="TestFeature"
_rootFrameName = "Test_RootFrame"
_newFrameName ="Test_FrameDuplicate"
_positionsX = 7
_positionsY = 4
_positionsZ = 1
_distanceX = 36.0
_distanceY = 62.4
_distanceZ = 0

_comp = getComponent()
 
print("creating frames")
for num in range(1,3):
  createFramePattern(_rootFeature, _rootFrameName+str(num), _newFrameName+str(num)+"_",_distanceX, _distanceY, _distanceZ, _positionsX, _positionsY, _positionsZ)
_comp.rebuild()

Thanks for the help :slight_smile:

1 Like