Copy a group and create a new .skp file and add the group to it with ruby


#1

I’m trying to create a plugin that does a context click on a group and sends it to a new instance of sketchup for editing (some editing done by code). i use the UI.savepanel to get the path and create the skp file and open it with the UI.openURL() or Sketchup.open_file(). how do i get a handle on the new model instance to continue with the code functionality.

Note:
the Sketchup.open_file() asks to save and closes my original model before i get to the new model instance
where the UI.openURL() does not ask to save nor closes the original model before opening the new instance.

it seems i can get a @second_model = Sketchup.active_model handle with the Sketchup.open_file() method.
Probably because it has to close the original model first.

but how to copy a group over to the new instance? any Suggestions?


#2

Save the definition as a SKP file (http://ruby.sketchup.com/Sketchup/ComponentDefinition.html#save_as-instance_method) - then you can launch a new instance of SU (if you are on Windows) to open the SKP file. To launch a new instance of SU you need to find the install location of SU. If you are on Mac you cannot open a new instance, but you can open a new model window.

Btw, why do you need to start a new instance of SU for editing made by code? Why not edit the group in-place?


#3

On my PC if I use:
UI.openURL("file:///#{full_path_to_some.skp}")
It opens it in a new instance of SketchUp and leaves the existing model open.

Assuming that is what you want…
In the original model have your ruby code start an operation, make a component of the group and do a save as on it as “full_path_to_some.skp”, then abort the operation, the group is back as it was, now open the “full_path_to_some.skp” as outlined above and you are editing the group’s contents in the separate SKP.

I don’t see why your code can’t simply edit using the group’s entities context “in place”, separating what it does from the rest of the model…
If you want to keep a ‘backup’ of the starting group then make a copy [in code] and use make_unique on it ensure it’s not overwritten…


#4

to get the ruby to continue into the spawned model, you need to stay in the same SU instance…

this is part of one of mine…

     # assume grp and new_skp are defined elsewhere...
       ins = grp.to_component
      defn = ins.definition
      # named only to check if it's been undone...
      defn.name = "Explode Me Later"
      defn.save_as(new_skp)
      # revert original to last save...
      count = 0
      20.times do
        if Sketchup.active_model.modified? == false && Sketchup.active_model.tools.active_tool_name != 'CameraZoomTool'
          p count if count > 3
          break
        else
          count += 1
          Sketchup.undo
        end
      end
      Sketchup.open_file(new_skp) 
     # add code here that runs in the new model...

john


#5

It’s not so much for editing the group. it’s for setting it up in a new model environment to prepare it for sending to layout for dimensions.

I design custom steel parts that all fit together like a puzzle. once the entire puzzle is made then i currently take each piece and save it in a .skp template with some scenes and styles

each puzzle piece can have multiple smaller pieces that i need to spread out to prepare it for layout drawings.
so this is why i place it on it’s own in a model. i’m just trying to do it faster and more efficiently with code.

I’m currently saving the group into a new .skp file, but i want the code to create the standard scenes and do a few operations in the new model. i don’t think it’s possible to do transformations on a .skp file that is not opened.

so this is why i’m trying to start an operation in the original model that continues the code over into the new model. such as creating the standard scenes and applying a few transformations.

I will try john_drivenupthewall’s method of continuing the ruby in the newly opened model. i hope i have explained well enough what i’m trying to accomplish. and thank you for your responses

BTW thomthom it was good to meet you at BASECAMP in Steamboat Springs back in June


#6

Is this the only way to get ruby to the new model? can the ruby not continue to the Model opened with UI.openURL()?

Yes, I want to keep the original model open, i just don’t know if it is possible to continue / transfer the ruby operations in the ‘Spawned Model’


#7

on a PC you open a new instance of both SU and a new Ruby interpreter…

I guess you could write any needed variables to file for reading in the second instance or write them to the new model itself…

but you would need an observer that looks for a ‘tag’ to know when to run your ruby…

it could get messy fast…

in mine I basically open a full copy of the original or pass the selection from to it for processing…

the snippet comes from a bigger ruby that is a small part of an extension…

john


#8

You could as part of starting the new SU process pass in some arguments - to execute a given Ruby script for instance: http://www.thomthom.net/thoughts/2013/04/sketchup-command-arguments/

Sketchup.exe -RubyStartup "c:\path\to\My Script.rb

#9

I didn’t know you could do that. I assume that this script would run before extensions are loaded?


#10

hm… I am not 100% sure of what the order of execution is. Think you have to try and see.


#11

Interesting. Here is the order:

  1. Extensions Loaded
  2. WebDialog from extension is opened, then UI.messagebox from test extension.
  3. Script with UI.messagebox is executed.
  4. Extension Toolbars are displayed. (only after closing all message boxes)

#12

Sorry, I meant the order of which SU executes the command line argument.


#13

Looking at the source code it looks to be loading the RB file in the command line arg after it’s done loading the extensions.