hexagon logo

How to add rows to existing spline with Python?

I am a beginner in the "Python in Adams" world. I would like to create a function which makes it possible to add (and later remove) a given number of rows to an existing spline.
 
As I sometime have to copy/paste splines into Adams, it happens that I have to add (or delete) a large amount of rows before I can do so in order to match the length of the spline with the new input. Doing that manually is not fun if that number is big so I thought this could be a nice first tool to code in Python.
 
I started to write a function called "add_lines".
The inputs are:
  1. spline_id: id of the spline to be updated
  2. n_lines: number of lines to be added
 
It works fine until I try to add the new empty rows. By empty I mean [0,0]. My approach doesn't seem correct and I am wondering how I should proceed.
 
I am able to access the spline of interest and get the current values. But I don't know how to add or remove lines. Here is what I tried to do:
iSpline[spline_id].x=iSpline[spline_id].x+[0]*n_lines
 
The error I get is "ERROR: The number of X values for spline .MODEL_1.SPLINE_1 must be the same as the number of Y values."
 
I understand what the problem is (when I do that X is longer than Y) but I am wondering what the correct way of doing it would look like.
 
Thanks for the help.
  • One techniqe I used in the past was to build the spline x and y columns by creating a string tuple from the numbers with commas in between.
     
    After that it's easy to do a
    command = "data_element modify spline spline_name=%s" % (spline)
    command = command + " x=" + str(x)[1:-1]
    command = command + " y=" + str(y)[1:-1] 
    command = command + " linear_extrapolate = yes"
     
    Appending points can be done in several ways.
    I usually prefer reading model.spline.xs/ys into a real variable, modify that variable and then perform the above modify command with x=(eval(variable.real_value))
     
    If you want to get rid of huge splines, simply assign dummy values to the spline like
    command = "data_element modify spline spline_name=%s" % (spline)
    command = command + " x= -2,-1,0,1,2"
    command = command + " y= -20,-10,0,10,20" 
    command = command + " linear_extrapolate = yes"
     
    Note x/y must have the same length and at least 5 numbers.
     
     
     
  • Thanks Martin. I ended up doing something similar to what you suggest: using cmd language in my Python code:
     
    command ='data_element modify spline spline=.MODEL_1.{0} x={1} y={2} linear_extrapolate=no '.format(spline_name,newXvalues, newYvalues)
    Adams.execute_cmd(command )
     
    I still did not find a way to do that directly with Python code.