Creating frames in python

I am trying to create a frame in python. However although it appears in the graphics window, I cant interact with it in anyway. It also doesnt appear in list of items under the root of the component. This code is directly copied from the help.

from vcScript import * 
import vcMatrix as mtx 
   
app = getApplication() 
comp = getComponent() 
   
#simple demo that clears geometry sets in root node 
comp.Geometry.clear() 
gs = comp.Geometry.createGeometrySet(VC_FRAMESET) 
   
#create full circle of frames 
degrees = 360 
for i in xrange(degrees): 
  gs.createFrame(mtx.new()) 
  gs.update() 
     
for i in xrange(gs.FrameCount): 
  frame = gs.getFrame(i) 
  frame.translateAbs(1000.0, 0.0, 0.0) 
  frame.rotateAbsY(degrees) 
  degrees = degrees - 1 
  gs.setFrame(i, frame) 
  gs.update() 
     
app.render() 

Ok I figured this out.
You need to create a series of frames as features and type VC_FRAME under the root node and not a new geometry set of type VC_FRAMESET

framer = comp.RootFeature.createFeature(VC_FRAME,'frame') 
1 Like

although oddly it doesnt put the frame in the correct position until you turn the visibility on and off. I have tried turning it on and off in the code but it only does it when you manually click it on/off. Somehow the graphics screen does not update correctly.

from vcScript import * 
import vcMatrix 


mtx = vcMatrix.new()
app = getApplication() 
comp = getComponent() 

gs = comp.Geometry.createGeometrySet(VC_FRAMESET) 
framer = comp.RootFeature.createFeature(VC_FRAME,'frame') 
geo_f = [x for x in comp.RootFeature.Children if x.Type == VC_FRAME] 
for f in geo_f: 
  mtx = vcMatrix.new()
  mtx.translateRel(1000,500,500) 
  f.PositionMatrix = mtx 
  print f.Name, f.PositionMatrix.P.X ,f.PositionMatrix.P.Y , f.PositionMatrix.P.Z 
  f.Visible = False
  getApplication().render()
  #f.Visible = True
  getApplication().render()
  

 
getApplication().render()

1 Like

Hi,

The method .rebuild() does the same thing.

 

 mtx = vcMatrix.new()
  mtx.translateRel(1000,500,500) 
  f.PositionMatrix = mtx
  f.rebuild()

regards

In op, you are working at geometry set level not feature level. Frame feature is not the same thing as a Frame set.

Here is a video that explains the difference (link expires in 180 days)

https://visualcomponents.sharefile.com/d-s8e4d66ce5ac4461b

 

Thanks for that great explanation.
Basically I am exporting frames from solid works to csv format.
Then importing them to visual components.
I have for the code working except it doesnt display the frames I have created until I do something else like run and reset the simulation or turn visibility on/off.
Here is my code…any ideas?

from vcScript import * 
import vcMatrix
import csv
import os

# create a new empty matrix
mtx = vcMatrix.new()
app = getApplication() 
comp = getComponent() 

# set the directory and filename to be imported
os.chdir('c:\\temp') 
filename = "Dumb waiter v1.csv"

with open(filename) as csvfile:
   
  readCSV = csv.reader(csvfile, delimiter=',')
  # read in the file into rows and then find the names and coordinates
  for row in readCSV:  
    print row
    framerNamer = row[0]
    x = float(row[1])
    y = float(row[2])
    z = float(row[3])
    rotX = float(row[4])
    rotY = float(row[5])
    rotZ = float(row[6])
    # create a new frame with the imported frame name
    framer = comp.RootFeature.createFeature(VC_FRAME,framerNamer) 
    # find the frame with the new imported name in the geometry set
    
    geo_f = [j for j in comp.RootFeature.Children if j.Type == VC_FRAME]
     
    for f in geo_f:   
      if f.Name == framerNamer:
        mtx = vcMatrix.new()
        # apply the rotations for the frame orientation around the world frame
        mtx.rotateAbsX(rotX)
        mtx.rotateAbsY(rotY)
        mtx.rotateAbsZ(rotZ)
        # move the frame to its x y and z coordinates import from csv
        mtx.translateAbs(x,y,z) 
        # rotate it around the world axis x = 90 degrees as solidworks uses Y
        # for vertical
        mtx.rotateAbsX(90)
        # apply the new matrix to the frame
        f.PositionMatrix = mtx 
        # rebuild the geometry so the frame appears in the right place
        f.rebuild()

app.render()

Try a rebuild of the component, vcComponent.rebuild().

Here is a link (expires in 3 months) to a video where I show you a different approach using a vcLayoutSchemaPropertyList to create frames in the 3D world. In most cases, you only work with the schema object (define and edit it) using API – do not use the GUI.

https://visualcomponents.sharefile.com/d-sa3b5ccfd1ca4a739

If you ever use schema be aware of the following issues as of 4.1 release:


#1
Frames might be recreated if you attach layout item to different node.

#2
PositionMatrix property might not work at all.
Workaround would be to create schema property that defines offset.

#3
Lines connecting frames might be buggy.

#4
Issue with script parsing and complier, so consider using def OnFinalize() to safely generate the schema when loading layout.


So in the attached layout, I modifed the code I showed in the video to use the OnFinalize() event.

NB! Not sure if others are using 4.1, but a workaround for having a separate data.csv file would be to pack the file with the layout using the 4.1 pack folder feature, and then unpack it from auto-generated temp folder.

Terms:

schema object - a vcLayoutSchemaPropertyList object

layout item - an object that exists in a layout, such as drawings, annotations, dimensions and bill of materials

Test-Schema-Layout-Item-using-CSV.vcmx (9.04 KB)

data.csv (85 Bytes)

app.Simulation.update()
seems to cure the visibility problem.

Another trick is sometimes to set or clear selection of the application’s selection manager.

I have run through you video and its handy to know.
However, for the purpose for which I was doing this was to have an lifter or conveyor modelled in another cad system parametrically. Then import the geometry to set up a basic component and then be able to update the paths inside with frames generated from Solid works in this case.
Therefore when the geometry gets translated for a larger or smaller model then it would be simple to update the paths.
In the schema method it seems that these cant be directly used with a path in a component due to the frames being in the layout rather than the component. well as far as I can tell anyway.

Understood. Yes, Frame features is the way to go. Schema frames are considered robot positions, so they cannot be referenced in Path behavior.

You shouldn’t need to use the vcSimulation.update(). That’s an overkill.

Rebuilding the frames and rendering should be enough. Like in the snippet below:

from vcScript import *
import vcMatrix
comp = getComponent()
app = getApplication()
frames_trans = comp.RootFeature.createFeature(VC_TRANSFORM, 'Frames') 
frame1 = frames_trans.createFeature(VC_FRAME, 'F1') 
frame2 = frames_trans.createFeature(VC_FRAME, 'F2') 
mtx = vcMatrix.new()
mtx.translateRel(200,300,400)
mtx.rotateRelZ(45)
frame1.PositionMatrix = mtx
mtx.identity()
mtx.translateRel(-200,300,400)
mtx.rotateRelZ(-45)
frame2.PositionMatrix = mtx
frame1.rebuild()
frame2.rebuild()
app.render()

 

1 Like

Thanks for this! I was looking for this exactly saved me a bunch of time.

1 Like