Skip to content

Python API

Scripting interface for Sim4Life

142 Topics 441 Posts
  • How to compute the length of a spline using the Python API

    spline python api
    2
    1 Votes
    2 Posts
    2k Views
    PeterStijnmanP
    Works perfectly, thanks! Edit, I think you could also get away with: import s4l_v1.model as model import XCoreModeling def get_spline_length(spline): wire = XCoreModeling.GetWires(spline)[0] return wire.GetLength()
  • Entities Translation Transformation

    4
    0 Votes
    4 Posts
    1k Views
    SylvainS
    The behavior you are describing is a related to a not-so-uncommon misunderstanding of the .Transform property. Model entities in Sim4Life have a Transform property (which you access by obj.Transform, for example). This property holds the transformation matrix between the initial object and its final position. This means that it holds the composition of all the transformations that have been applied to this object since its creation. So when you write something like obj.Transform = Rotation(axis, ori, ang), it does not just rotate the object - it replaces the transformation by a simple rotation, erasing all previous displacements. To apply a rotation, one should instead use obj.ApplyTransform(Rotation(axis, ori, ang)). The same is true for applying a translation: first define a Translation object (which is usually independent of the Transform property of the object itself), then apply the transformation. For example: from s4l_v1.model import Vec3, Translation, Transform import numpy t = Translation(Vec3(-20, -16, 10)) # for a translation r = Rotation(Vec3(0, 0, 1), numpy.pi / 4) # for a rotation of angle pi/4 around the z-axis case = model.CreateSolidBlock(Vec3(0, 0, 0), Vec3(40, 16, -140)) case.ApplyTransform( t ) case.ApplyTransform( r )
  • Python API changes from SEMCAD 14.8 to SEMCAD 17

    Unsolved python api grid simulation
    2
    1 Votes
    2 Posts
    2k Views
    SylvainS
    The gridding algorithms are quite different in V17 compared to V14.8, so many options simply do not have a one-to-one conversion. The best strategy is probably to learn how to use the gridder in the GUI of SEMCAD 17 (in particular the Manual Grid Settings) and then use the "To-Python" function to auto-generate the corresponding Python scripts. I don't know V14.8 well enough to answer your other questions from the top of my head and my current V14.8 installation is acting up, so I can't help with your other questions right now. I will as soon as I fix my setup!
  • Is the function "To Polyline" available from the API?

    python
    2
    0 Votes
    2 Posts
    897 Views
    brynB
    I had a look but also did not find it, so I added a function ConvertToPolyline which will become available in the next release (you can test it internally next week).
  • 0 Votes
    1 Posts
    837 Views
    No one has replied
  • Planar cut using Python

    python modeling
    3
    0 Votes
    3 Posts
    2k Views
    SylvainS
    You're welcome! Note, however, that functions that are not part of the s4l_v1 module are not checked for backward compatibility between Sim4Life versions and their signatures are in principle allowed to vary (although this is quite rare, in practice). It is recommended to always try to use functions under the s4l_v1 module when possible, since only these are "officially" part of the API. This said, if you need to bend an object in Sim4Life v4.x or lower, use XCoreModeling.PlanarCut :-)
  • This topic is deleted!

    1
    0 Votes
    1 Posts
    3 Views
    No one has replied
  • Creating a CSV file from an algorithm's output table

    python api table csv
    1
    1 Votes
    1 Posts
    1k Views
    No one has replied
  • 0 Votes
    1 Posts
    779 Views
    No one has replied
  • 0 Votes
    1 Posts
    1k Views
    No one has replied
  • 0 Votes
    1 Posts
    2k Views
    No one has replied
  • Example of mask filter in postpro?

    python mask filter sar
    3
    1 Votes
    3 Posts
    2k Views
    pcrespoP
    Thanks so much!
  • Example using the model.Extrude function

    python extrude
    1
    0 Votes
    1 Posts
    723 Views
    No one has replied
  • Creating coils via Python

    python coils
    1
    1 Votes
    1 Posts
    807 Views
    No one has replied
  • How can I interpolate the last snapshot of data in thermal simulation

    Solved
    6
    1 Votes
    6 Posts
    2k Views
    A
    @sylvain Never mind, I figured it out why. Thanks anyway
  • How can I install a new python package within Sim4Life

    3
    0 Votes
    3 Posts
    1k Views
    pcrespoP
    @sylvain Yes! thx
  • How can I read matlab export as a multi-dimensional array?

    2
    1 Votes
    2 Posts
    1k Views
    SylvainS
    In Matlab, you can reshape the Snapshot0 quantity in the following way. For cell-centered data with 3 components (e.g. Electric field E(x,y,z)): E = reshape(Snapshot0, [length(Axis0)-1, length(Axis1)-1, length(Axis2)-1, 3) which you can then use as: E[i,j,k,c] where c is an index to the field component and i,j,k are indices to the discretization of space. For point data with 1 component (e.g. Vector potential V(x,y,z)): V = reshape(Snapshot0, [length(Axis0), length(Axis1), length(Axis2)) which you can then use as: V[i,j,k] where i,j,k are indices to the discretization of space.
  • 0 Votes
    2 Posts
    915 Views
    SylvainS
    The function you are looking for is LinkMaterialWithDatabase(). This is how you can add new material settings with properties taken from the database import s4l_v1.document as document import s4l_v1.model as model sim = document.AllSimulations[0] for ent in model.AllEntities(): mats = sim.AddMaterialSettings([ent]) mats.Name = ent.Name sim.LinkMaterialWithDatabase(mats, str(ent.MaterialName)) To modify the settings of an existing simulation and update their properties, you can do something like this: import s4l_v1.simulation as simulation material_settings = [s for s in sim.AllSettings if isinstance(s, simulation.fdtd.MaterialSettings)] for mats in material_settings: material_name = mats.Name sim.LinkMaterialWithDatabase(mats, material_name)
  • How to remove warnings from deprecated functions?

    python
    1
    0 Votes
    1 Posts
    542 Views
    No one has replied
  • how to set the weights of field combiner in python?

    Moved Solved
    4
    1 Votes
    4 Posts
    1k Views
    SylvainS
    sorry, I gave you some code for the Simulation Combiner, not the Field Combiner... This is how you can use the GetWeights and SetWeights functions for the field combiner: >>> field_combiner.GetWeights() ((1+0j), (1+0j)) >>> field_combiner.SetWeights( ( (2+0j), (-3+0j) ) )