Model.import skp file without activate the component tool

Hi, the model.import function supports skp file. I know it is used for component, so the user may want to immediately place the component after import.

However, in my case, export to skp and import skp is used for interchange between different users or project cases. And I always handle the coordinates correctly, so I always just want to place it in the original place. Is there a way, I can get rid of the place component tool after import skp?

Thanks,

Han

Rather than using the Model#import method you can use DefinitionList#load. Once you have a component definition you can use Entities#add_instance with the identity transform for position to line up the imported component with the model axes.

Thanks, I have just figured out it, but I’m using Sketchup.active_model.entities.add_group(defs.entitis.to_a) for this. I think the approach is similar

I’m not sure how you are using add_group here. Are you trying to move entities from within one component definition into the group’s drawing context? For that I’d place a new instance of the component in the group and explode it. add_group with arguments can be messy and sometimes crash SketchUp if the entities are in the wrong drawing context.

I have experienced that, I think you are right. add_instance and explode will do the things better.

Another approach could be to simply keep it a component. This allows the user to save out any changes over the existing file or reload the file if it has changed. A component is more or less an embedded file so it might make the most sense to use a component.

In fact, the concept of component have made a lot of mess for me, because it is harder in my program internnaly to accuractely extract the position of vertices. For example, when doing texture mapping, I managed the positions and orientation of some aerial images and all the texture mapping is used per face. But I’m struggling with correct ones for component.

Group or component should make no difference regarding coordinates. If the instance has the identity transform there shouldn’t be any difference to having the entities lose in the model root either.

I just don’t quite understand the logic of Vertex.position. It seems that it changes when in or out a component. And also the Group.transformation.to_a also changes.

FYI: There is a send ESC key hack. See example written for importing a .3ds file. It can be changed for a .skp file as well.


But, as Julia said, it makes more more sense to use the DefinitionList#load and Entities#add_instance workflow.

They do. When you are in the same drawing context as an entity (or if it is in a parent drawing context to where you are) SketchUp returns and expects all coordinates related to it to be in the global coordinate system. In other cases SketchUp uses the actual local coordinates.

it isn’t transparent or easy to understand and I still don’t know why SketchUp does this. However, in the special case of the group/component transformation being the identity matrix, which it is if the component is in “the original place”, it makes no difference. In such case local and global coordinates are identical.

WOW, I’m not aware of this, is it always after import something, the last definition is the last imported model? For 3DS, I’m currently import by self using assimp the PolygonMesh.

That sounds like a dangerous assumption to make.

There is no API contract for that - that’s just an implementation detail that might change. I’m not even sure you can completely rely on that with current version.

All true. I wrote that example when v2015 was current.

However, there is always a easy way to get the newest added object to a collection.

(1) take an array snapshot of the collection before the command …

before = model.definitions.to_a

(2) After the addition of a new object(s) use the Array#- method …

added_comps = model.definitions.to_a - before

In the case of that example the added_comps array will have only 1 member.


FYI, I logged a FR in the API tracker.

So, @saedrna, in the example’s import_3ds_to_point method, the following code snippet …

      mdl = Sketchup.active_model
      result = mdl.import(path)
      if result # returned as soon as definition is loaded
        send_escape() # to cancel placing with the mouse
        cdef = mdl.definitions[-1]
        cinst = mdl.active_entities.add_instance(
          cdef,
          transform
        )
      end

… would change to …

      mdl = Sketchup.active_model
      before = mdl.definitions.to_a # array snapshot of definitions collection
      result = mdl.import(path)
      if result # returned as soon as definition is loaded
        send_escape() # to cancel placing with the mouse
        added = mdl.definitions.to_a - before
        cdef = added.first
        cinst = mdl.active_entities.add_instance(
          cdef,
          transform
        )
      end

As I’ve said before there is no need t use Model#import, if you are loading an .skp. In the more general case of importing non-skp files and get a reference to the definition I’ve filed a request for that.

And I again agree. It is the easiest and most straightforward approach and uses the least code.

Thanks for the reminder. I’ve cross-referenced my recent request to this, as they could be implemented at the same time. (FR bundling.)

2 Likes