Somebody help me to make simple plugin for this code

Thanks you all you guys in this wonderful forum . It’s very useful for me to learn many things from here
I’m just a newbie of sketchup ruby .
When I did search here I found this nice code , I tried to make it to ‘Plugin’ but very hard . Would somebody please make this to small plugin for me .
Many thanks

def self.copy_entities(tr, entarray)

  # Copies and transforms an array of entities
  model = Sketchup.active_model # Open model
  ent = model.entities # All entities in model

  if entarray.length == 0
	UI.messagebox "Select object"
	return
  end
  tempgroup = ent.add_group entarray
  temptrans = tempgroup.transformation
  tempdef = tempgroup.definition

  newgroup = ent.add_instance tempdef, temptrans


  ent.transform_entities(tr, newgroup)
  newgroup.explode
  tempgroup.erase!
end #def

#test using selection entities

model = Sketchup.active_model # Open model
ent = model.entities # All entities in model
sel = model.selection # Current selection
ss = model.active_entities
entities = model.active_entities
# define some delta transform


model.start_operation "Erase Inside"
ent_face = entities.grep(Sketchup::Face).find_all
ent_edges = entities.grep(Sketchup::Edge).find_all
ent_face.to_a.each { | entity| entity.erase! }
ent_edges.to_a.each { | entity| entity.erase! }

ent_group = entities.grep(Sketchup::Group).find_all

ent_group.to_a.each { | entity| entity.explode }

vector = Geom::Vector3d.new(0.m , 0.m, 0)
tr = Geom::Transformation.translation(vector)

result = self.copy_entities(tr, sel)
model.commit_operation
model.close_active until model.entities == model.active_entities
#  Add menu
unless file_loaded?(__FILE__)
    menu = UI.menu('Plugins')
    menu.add_item('ATI') {
    self.create_cube
  }

    file_loaded(__FILE__)
end

Please format the code according:
how-to-colourize-code-on-the-forum

BTW the original code is here:

1 Like

Begin here …

1 Like

Thanks you dezmo, that is right, i leard from that , and it helps , still confused, by the way i will try agian , thanks you.

Yes thanks you DanRathbun , i have read many things about the link which you give to me. And i did try many times to put it in Plugin tab column to use, but it doesn’t work . So would you please take a little time to make it for me like a small sample for me,. You are great , many thanks

hello_cube example

2 Likes

Thanks you dezmo , i have read many tutorial from mr.thomthom as well. Just make like his tree of code to make plugin , but it doesn’t work, some time i copied his example as well , but the same,… Thanks agian

I don’t know what this means!

  1. Your code snippet above is not formatted correctly.
  2. It is not within namespace module and within a plugin submodule.
  3. The menu item calls a non-existant method (create_cube)
  4. There is code that will run when the snippet is loaded (outside of any method).

You need to learn basic Ruby programming first.

A forum is not a good place to teach programming.

I refer you to the wikilists linked above and the example dezmo linked.

1 Like

(upload://w7yPZpEMHJq8kGiC7kAxTfozrYu.rb) (2.4 KB)

Thanks DanRathbun so much .
I did try many times to put the code in Plugin Tab of Sketchup program but it doen’t work at all .
I’m learning basic Ruby programming and wikilist too , but this code is temporary useful for me , so it would be nice if somebody can help for me a moment. it is really appriciate
Once again , thanks you so much.05a.rb (2.4 KB)

I just find this link in my collection, it is old but maybe helps… :wink:

turn-your-plugin-into-extension

And “the official” one:
https://developer.sketchup.com/hello-cube

1 Like

I have tried to do like the Creat Cube of Mr Thomthom but it doesn’t work . I check for namespace and the tree of code as well, but something wrong with selection or somethings, thank you.

Yes, definitely something wrong with something.
image
:stuck_out_tongue_winking_eye:

1 Like

Simple quote but the best :wink: , i “do” for this code to make simple plugin for 5 days already , but doesn’t work still now. Yes, i do , i do , i do … ^^

With regard to “05a.rb” …

A. The namespace module ( module Examples ) is not YOUR module.

ALL of YOUR code, plugins and extensions MUST be inside YOUR toplevel namespace module.
It must be a unique name that separates ALL or YOUR codes and extensions from everyone ELSE’s extensions. Ie …

module ATI
  # ALL of YOUR extension submodules inside this toplevel module !
end

B. The submodule ( module Examples::HelloCube ) is NOT YOUR extension name, and has nothing to do with copying entities. IE, it should be …

module ATI
  module CopySelection
    # ALL of YOUR extension's code inside this submodule !
  end
end

C. The menu item you create has menu caption text that is "Create Cube Example" that has nothing to do with copying entities.


D. The comments in YOUR code are not sufficient to tell us what YOU want your code to do.
(Many of the comments are left over from the “Hello Cube” example.)

Add more comments to your code explaining what it is supposed to do.


E. As I said above, you have code at the submodule level outside of any method.
This code will run only once, when the module is first loaded.

This code is from line 31…56 …

    #test using selection entities

    model = Sketchup.active_model # Open model
    ent = model.entities # All entities in model
    sel = model.selection # Current selection
    ss = model.active_entities
    entities = model.active_entities
    # define some delta transform


    model.start_operation "Erase Inside"
      ent_face = entities.grep(Sketchup::Face).find_all
      ent_edges = entities.grep(Sketchup::Edge).find_all
      ent_face.to_a.each { | entity| entity.erase! }
      ent_edges.to_a.each { | entity| entity.erase! }
      #  ent_group = entities.grep(Sketchup::Group).find_all
      # ent_group.to_a.each { | entity| entity.explode }


      vector = Geom::Vector3d.new(0.m , 0.m, 0)
      tr = Geom::Transformation.translation(vector)

      result = self.copy_entities(tr, sel)
    model.commit_operation

    model.close_active until model.entities == model.active_entities

E1. Also this code ERASES any edges and faces BEFORE they can be copied.

E2 This code does NOT need find_all after the grep.

E3. The #grep method returns an array, so you would not need to call #to_a on the result of a #grep call.

E4. The transform …

     vector = Geom::Vector3d.new(0.m , 0.m, 0)
      tr = Geom::Transformation.translation(vector)

… is frivolous. It means do not move at all.


So …

F. Firstly, explain what your plugin is supposed to do, in a comment at the TOP of the CODE.


:question:

ADD: You still have not fixed the first post’s code block.

2 Likes

In addition, although (contrary to what you wrote) your menu item is added to the Extensions menu, the code block attached it will throw an exception, as it calls copy_entities without any arguments whereas the method requires two. It is completely unclear where you will get those arguments when the menu item is clicked.

2 Likes

Thanks you so much for your kindness of instruction . finally i edit and finish my small plugin wohoo!
Now i understand more about def method and call it back to the menu_item . I delete to_a and find_all , and it works well too .

Just a little bit worry that newgroup which i copied from temporary “group(component,then erase)” will change the attribute? but it seem works fine .

So from your advice i will learn more about wikilist and basic ruby more ( and colourize code in forum too :slightly_smiling_face:)

Once agian , thanks you so much Mr. DanRathbun , you are so awesome ^^

Yes, i understand your notice, and i re-edit and it works fine now, thanks you.

Again, … you haven’t explained what you really wanted to do, nor have you shown us the “fixed code”.

Also many years ago, I posted a template for a basic extension …

My purpose is : I come to one’s group entities put a component to it , and then erase edge and faces of it(group) , and erase by itself(component) , newcomponent copy will be outside the group when group=nil(delete)
(Like replace group by component which is same like replace component attribute)
06.rb (2.5 KB)

Once agian thanks you so much Mr.DanRathbun