Removing 2D entities from skp using script
-
Like consider this skp file
http://sketchup.google.com/3dwarehouse/details?mid=7c756f37a95eb7af929c37a6b1ce275b. I should be able to identify that it has a 2d image and not a 3d model and should be able to delete the entity. Sometimes such 2d images also have the "face me" behavior.
Sry for not being clear the last time. -
A two step method would catch 99% of cases:
Step one: just check the bounding box. If it's 2-D, so is its contents. If not, it could still be a flat object rotated relative to the container's axes. So...
Step two: Loop through the faces in a group/definition and check their normals. If they're all parallel, it's "flat".
What these wouldn't catch would be something like a deck of 2-D cards, with individual faces - all parallel - on different planes. You could add a third step to check for non-coplanar faces, but the first two would probably be sufficient for what I think venommax's needs are.
If nobody codes it in a week, I can do it (deadline...).
-
There is difference between a 2D Image [an 'Image' is a special type of thing in SUp] and a '2D Object' - from your face-me horse link I think you want the latter...
There will be lots of 2D things in your model like 'faces' for example: again, I suspect you want to select only component-instances/groups. The quick way is to loop through the model's definitions and check for a 2D bounding box and then select all of the instances of these definitions...
Here's the code...def selectFlatInstances() model=Sketchup.active_model defs=model.definitions flats=[] defs.each{|d| bb=d.bounds if bb.height==0 or bb.width==0 or bb.depth==0 flats<<d.instances end#if } ss=model.selection ss.clear flats.flatten! flats.each{|e|ss.add(e)} end#if ### run by typing selectFlatInstances in the console, all flat instances [compos+groups] are selected - hit delete to remove them or do something with them otherwise - e.g. cut and paste-in-place in another model...
If you delete them remember to purge the unused components from your browser...
-
@TIG I tried both of your methods. But both didnt work. The
entity.class is a Sketchup::ComponentInstance and not Image. I do not wish to select the 2d faces manually. Like for the above example of "horse rider face me" The whole of the 2d component should be deleted with nothing in the sketchup file.Or Like in this case http://sketchup.google.com/3dwarehouse/details?mid=238e0a5acc8ef3dd5cb2b1363f4a4cab&prevstart=72, the model has a person standing on the side(whos is not 3d ). So he should be detected and removed. But the bed(3d) should be intact. I need this to do a batch update on a set of sketchup images. So everything should be automatic.Thanks again for all d help...
-
@venommax said:
@TIG I tried both of your methods. But both didnt work. The
entity.class is a Sketchup::ComponentInstance and not Image. I do not wish to select the 2d faces manually. Like for the above example of "horse rider face me" The whole of the 2d component should be deleted with nothing in the sketchup file.Or Like in this case http://sketchup.google.com/3dwarehouse/details?mid=238e0a5acc8ef3dd5cb2b1363f4a4cab&prevstart=72, the model has a person standing on the side(whos is not 3d ). So he should be detected and removed. But the bed(3d) should be intact. I need this to do a batch update on a set of sketchup images. So everything should be automatic.
Thanks again for all d help...If you download/copy my code from the earlier post and paste it into the file in your Plugins folder called something like
selectFlatInstances.rb
- note it must have a .rb suffix NOT .txt - then when you reopen SUp, if you typeselectFlatInstances
in the Ruby Console all 'flat' instances in the Model ARE automatically selected for you [I tried it on the sample you linked to, so I know it works]: you then just click on the SUp window top-bar to activate that window [rather than the console] and hit the delete-key to erase them all, and finally purge the now unused Components in your Component Browser... save and close...
Tip: please stop referring to them as "SketchUp Images" as that would be something quite different, and NOT we are dealing with here !
-
@venommax said:
[...] I need this to do a batch update on a set of sketchup images. [...]
@tig said:
[...]
Tip: please stop referring to them as "SketchUp Images" as that would be something quite different, and NOT we are dealing with here !
I think he meant "SketchUp Documents" meaning multiple model files.
-
If you want to process a series of SKP files outside of a model you need a tool that opens every file inside a folder and selects flat items and deletes them ?
TheselectFlatInstances.rb
will select them all for you so you'd just need to add an 'erase'.
in the form### open SKP model model=Sketchup,active_model selectFlatInstances() model.selection.to_a.each{|e|e.erase! if e.valid?} model.definitions.purge_unused ### save model model.save(model.path.tr("\\","/"))if model.modified? ### and 'close' BUT NOT scriptable BUT can 'open' next file - when all done UI.messagebox("All Done. Close this SKP") ???
The other part - to open each file in turn and do this is more difficult...
Easy to select a files within a folder, but as each one opens the ruby must run ?
This link has some clues http://forums.sketchucation.com/viewtopic.php?p=186916#p186916 -
You can iterate all .skp files in a folder, import the file as a component, then export it back again.
-
@thomthom said:
You can iterate all .skp files in a folder, import the file as a component, then export it back again.
Neat thinking...
-
Here's a script...
TIG (c) 2009
This processes all of the SKPs in a selected folder and deletes any 2D component definitions within them.
Use with care as it will delete 2D components within components etc within these SKPs.
It can even remove all the contents of a SKP that was entirely 2D geometry,
so it will ask if you want to delete those 'empty' files completely...
To use, open a new [preferably empty SKP model - although running it shouldn't affect the model you are in]
In the Ruby Console type batchEraseFlatInstances and press <return>:
In the dialog navigate to the folder to be processed and pick any SKP within it and press OK,
ALL SKPs in the folder are processed and it IS IRREVERSIBLE - be careful !!!
-
Thanx everyone and espp. TIG.. It has been my first post ever in any forum... This thread can be closed as the code by TIG solves the problem posted.
-
I think the defacto standard in this forum is that the original poster (e.g. you) edits his/her first post and adds [Solved] to the beginning of the title, to let people know there is a solution in the thread.
I could be wrong, though, since I'm also quite new here...
Advertisement