Learning to make a plugin/extension

this is just for educating myself. I only wanted to learn how to make plugins… There are reasons why I wanted to learn how to add functions such as removing or adding…

removing Chris and then adding a rectangle is a good way for me to learn how to add these functions.

sigh. Please just teach me, I would appreciate it.

Removing a certain entity based on PID is extremely specific. I’ve made almost 60 extensions and never done anything like that. Running code on model launch requires observers which is quite complex. What you would do in an actual plugin is to remove the selected entities when the user presses a button/clicks a menu entry.

For adding a rectangle, see Entities#add_face. Note thate there is no “rectangle entity” in SketchUp. What a user would create with the rectangle tool is a face entity with 4 edges forming a rectangle but once drawn SketchUp no longer recognizes as a rectangle.

alright. I am only just asking questions to know what’s possible and what’s not possible. You’ve answered all of the questions. But like I said “Keep this in mind. I am a beginner at programming”. I am really a beginner trying to learn how to programming.

Now I’ll take a look at “Entites#add_face”. And Can you provide more explanation of what do you mean: Once drawn Sketchup no longer recognize as a rectangle"

Are you saying when the rectangle is created because it creates a face based on 4 edges or 4 points is where Sketchup will stop seeing it as a rectangle?

For now, I want to know how to add a function like “remove/delete” and “creating/adding”. I haven’t learned how to add functions yet.

You need to read and understand the API methods…

You can add/collect edges into/from an ‘entities’ context [or even the edges that are parts of an arc/circle/polygon/etc] - but these do not automatically make a face.

If you have an array of ‘edges’ which you believe should support a face, then there are additional methods to add it…



You can also use an array of ordered points to make a face - which only works when they are coplanar:


The equivalent edges.vertices.uniq can also double up as ‘points’ when used in this ‘add’ method - as of course could an array of valid ‘points’.

It is actually a dynamic component instance, meaning that it is a Ruby object of the Sketchup::ComponentInstance class.

We have already posted a topic thread with code examples of finding an instance or group by name. It is in the Ruby API sub-category.

I apologized for the inconvenience that you may find this conversation to be. I warned you that I am a beginner. This means I am self-studying on how to programming. that’s why I appreciate your help with this self-learning. I could use a teacher to help me out by make sure I am on the right track.

Alright, I’ll look at these examples and the links that you guys have provided so far. I’ll keep doing some more studying until I finally understand how it works.

I too am a hobbyist coder. I have found that to learn any new object-oriented coding system you have to grok two things: 1. the language itself (such as Ruby), and 2. the framework for a specific domain (such as the SketchUp Application Programming Interface or API).
You don’t need to know it all, just enough to make things happen. You don’t need to memorize it all, just have a couple good web pages bookmarked so you can copy and paste method calls from them. I use http://ruby-doc.com/docs/ProgrammingRuby/ for basic Ruby methods, and http://ruby.sketchup.com/ for the SketchUp API methods. The latter is up-to-the-minute, the former is rather obsolete but it’s good enough for me.
To learn Ruby, I read “Learn to Program” by Chris Pine (hard copy book) and “why’s (poignant) Guide to Ruby” online. Lots of other great stuff out there too.
To learn SketchUp’s API, I read “Automatic SketchUp” which was a hard copy book but can now be found online. The official SketchUp examples are also a great starting point. I also studied some of ThomThom’s open source plugins as a style guide and practical example of real-world coding.
Invest in a good programmer’s text editor - I use TextMate on the Mac - that does things like color coding keywords and keeping track of indents. Makes catching mistakes that much easier.
Coding is hard, and you will make a ton of mistakes - it’s expected, which is why there’s a Ruby Console window in the first place. But when you finally get your code to work, it’s a dance-around-the-room-with-joy kind of feeling. I’m hooked!

1 Like


yeah, I learned that I need to know the API of SketchUp (SU) but I also need to know the basic of RUBY language. Well, what I meant is that once I learned something regarding the SU API, I will use it as a reference and look at the RUBY language to understand how it works.

Yes, I know about the ruby console which is very useful. That’s why I used a command line in the plugin so I can find out any mistakes I made:

Of course, It will take me through many trials and errors to get the functions I wanted to be working. That’s a way to learn from your mistakes. That’s why I mentioned I am a beginner. :slight_smile:

I know the feel of when you got something working and you jumped around the room and singing “Yahoooooo” or something else. I have felt that many time with modeling in SU when I got the shape correct as I wanted it to look like. I am sure you already know that nobody would become an expert in programming without going through trials and errors. The trials and errors would mean where you would have made a ton of mistakes, being frustrated, angry and yelling “why-why!”, and having a nightmare, going through the books and other research to find out why you are getting it right.

It happened to me and everybody. I had the experiences with modeling. I know I will go through that trial and errors again, but this time it’s with programming.

1 Like

Heck of a way to have fun, no?
Almost as bad as golf, another habit of mine. :wink:
Check out the resources I mentioned, they helped me a lot.
Good luck!

Yeah. These links are already mentioned above. Thank you for sharing your experiences and knowledge with me. :+1:

It works so much better the other way around.

On this page from the official Ruby documentation,

you will find a list of coding primer articles.

Print them out and take them into the toilet room, and make use of otherwise wasted time upon the “throne”.


(A tablet is even better.)

1 Like

The other way around? Is that a real fact that it works so much better? :thinking:

I’ll keep this in mind and haha :joy: yeah I use my iPhone to study the language of Ruby whenever I use the bathroom :toilet: + :iphone:

1 Like

Different people learn different ways. i’m a practical learner and wouldn’t be able to keep attention and remember theory without instantly applying it. Others, like Dan, prefer a solid base of theory to stand on.

I can’t say any way of learning is better than the other, it’s up to you what works best for you.

If we are still discussing how to get a reference to the Chris component instance you could use the following code.

Sketchup.active_model.entities.find { |e| e.respond_to?(:definition) && e.definition.name == "Chris" }

If there are multiple Chrises in the top level drawing context the one first drawn will usually be returned. I’m saying usually because deleting an entity and undo the deletion seems to change the order. If there is no Chris nil is returned.

1 Like

They don’t call it “back-asswards” for nothin’ :stuck_out_tongue_winking_eye:

haha alright. That’s fair enough! :grin:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.