Skip to content
  • Search
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse

ZMT zurich med tech

  1. Home
  2. Sim4Life
  3. Python API
  4. Importing and exporting coordinates for an array of points

Importing and exporting coordinates for an array of points

Scheduled Pinned Locked Moved Python API
15 Posts 3 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mkn
    wrote on last edited by mkn
    #4

    Thank you, @fanruifu!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mkn
      wrote on last edited by
      #5

      Thank you very much for your reply @bryn! I appreciate the help.

      1 Reply Last reply
      0
      • brynB bryn

        hi @mkn
        here is some example code that exports the currently selected points as a csv file:

        def dump_selected_points(fname):
            sel = XCoreModeling.GetActiveModel().SelectedEntities
            sel = XCoreModeling.CollectEntities(sel)
            with open(fname, "w") as file:
                for e in sel:
                    if isinstance(e, XCoreModeling.Vertex):
                        x = e.Position
                        file.write("%g, %g, %g\n" % (x[0], x[1], x[2]))
        
        M Offline
        M Offline
        mkn
        wrote on last edited by
        #6

        @bryn I am wondering if it is possible to specify groups of points rather than having to select the points manually. I have a large number of groups with points that I want to export to separate files, and selecting each and executing the code can be quite time consuming.

        Thanks again!

        1 Reply Last reply
        0
        • brynB Offline
          brynB Offline
          bryn
          ZMT
          wrote on last edited by
          #7

          the code will dump all points in a single file. this will work also if you select multiple groups.

          • sel = XCoreModeling.CollectEntities(sel) will recursively add all children of selected entities
          • if isinstance(e, XCoreModeling.Vertex): will filter out any non-Vertex entities, like groups, meshes, etc.
          1 Reply Last reply
          0
          • M Offline
            M Offline
            mkn
            wrote on last edited by
            #8

            Thanks @bryn! That will dump them all points in a single file and I want the points in each group to be saved in a separate file. What I would like to do is carry out the same operation but for a certain group name rather than through selection.

            1 Reply Last reply
            0
            • brynB Offline
              brynB Offline
              bryn
              ZMT
              wrote on last edited by bryn
              #9

              @mkn
              this could be done in different ways, but how about the following:

              import os
              
              def dump_selected_points(dir):
                  for e in XCoreModeling.GetActiveModel().SelectedEntities:
                      dump_recursively(e, dir)
              
              def dump_recursively(entity, dir):
                  sel = XCoreModeling.CollectEntities([entity])
                  with open(os.path.join(dir, entity.Name + ".txt"), "w") as file:
                      for e in sel:
                          if isinstance(e, XCoreModeling.Vertex):
                              x = e.Position
                              file.write("%g, %g, %g\n" % (x[0], x[1], x[2]))
              
              1 Reply Last reply
              0
              • M Offline
                M Offline
                mkn
                wrote on last edited by
                #10

                Thanks @bryn! Just to confirm directory here would be a list of text file names, correct?

                Also, would it be possible to add the points from each group to a list while saving them to file? Sometimes I would need to do this to create splines or polylines using those point groups.

                Thanks again!

                1 Reply Last reply
                0
                • brynB Offline
                  brynB Offline
                  bryn
                  ZMT
                  wrote on last edited by
                  #11
                  1. os.path.join(dir, entity.Name + ".txt") is a file path inside the directory dir. since the file name is taken from the entity name (depending on selection a group or some other entity), you should take care to assign unique names to the entities.

                  2. yes, just collect the points x and return them.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mkn
                    wrote on last edited by
                    #12

                    Thanks! This what I did when adding elements to a single list:

                    pts = []
                    for e in sel:
                    if isinstance(e, xcm.Vertex):
                    x = e.Position
                    pts.append(x)

                    I'm not sure how to append to different lists.

                    I appreciate the help!

                    1 Reply Last reply
                    0
                    • brynB Offline
                      brynB Offline
                      bryn
                      ZMT
                      wrote on last edited by
                      #13

                      if you return the pts from dump_recursively, you can collect a list of list of points like so:

                      list_of_pts = []
                      
                      for e in XCoreModeling.GetActiveModel().SelectedEntities:
                          pts = dump_recursively(e) # returns the points list from inside a group
                          list_of_pts.append(pts)
                      
                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mkn
                        wrote on last edited by
                        #14

                        The code runs and 'txt' files are saved with the point coordinate, but 'list_of_pts' comes back empty for some odd reason.

                        1 Reply Last reply
                        0
                        • brynB Offline
                          brynB Offline
                          bryn
                          ZMT
                          wrote on last edited by
                          #15

                          @mkn how about you post your code. maybe somebody will show you where the bug is

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Search