How do you make plug-ins for sketchup
-
I have no idea, I'm just learning the sketchup API; maybe Chris will come by and give you his input.
... Anyways, now I'm trying to figure out how to make a transformation tool like the move tool. Any tips?
-
@unknownuser said:
I have no idea, I'm just learning the sketchup API; maybe Chris will come by and give you his input.
Oh goodness, I don't know that. Adam is part of the brains around here. I was hoping to learn from the responses from one of the other programmers. I'm just a hack, I really know very little about the technical ins and outs. Jim posted an interesting idea that I think is related to the question though. It can be found here:
http://www.sketchucation.com/forums/scf/viewtopic.php?f=180&t=18522#p151220
@unknownuser said:
... Anyways, now I'm trying to figure out how to make a transformation tool like the move tool. Any tips?
Transformations are still the bane of my life. Little by little I feel like I'm getting control of them, but that is only after a few rather involved transformation scripts. I think I keep writing them because I'm a glutton for punishment.
The idea is that you first create a transformation object. You then apply that to what you want to transform. There are lots of ways to make transformation objects. And different types of entities want to be transformed differently.
For a simple move, start with just moving whatever is selected. Make it so you select geometry, then run the script and it moves the geometry. Easy enough.
- define the selection to move
selection = Sketvhup.active_model.selection
- Define the transformation. It will be a translation transformation. this is the example in the API:
@unknownuser said:
vector = Geom::Vector3d.new 0,1,0
t = Geom::Transformation.translation vectorThe translation transformation needs a vector to determine how to move your object. The vector in the example is 1 unit on the y axis. You can make it 100 units on the y axis, 10 on x, and -200 on z. That would be [10,100,-200], for example. And it would move your selection from where it sits, that distance, on each axis.
3. Apply that transformation to your selection.
t = Geom::Transformation.new([10,100,-200]) entities.transform_entities(t, selection)
I have not tested the code there might be a glitch or two, but I think this will get you started.
Chris
- define the selection to move
-
A complete rip off of BTMs code:
model = Sketchup.active_model entities = model.selection # - - - - - - - - - - - - entities.each { |e| if e.typename == "Face" e.pushpull 10 end }
-
then change it around and make it erase all faces instead of pushpulling them
Chris
Edit: you literally just copied and pasted, awesome!
-
That's good, but my main issue (which I've been trying to figure out) is how to use the cursor to set the transformation, like the move tool does; click and drag. I thought I saw something on it once, but am still confused.
*I started writing this before remus's post. I see you used the 'selection' method, I guess that would fix the whole pushpull-resulting-faces issue I had.(that's what they're called, methods, right? I can never remember...)
*I started that last part before Chris posted and Chris, why won't it work to just replace e.pushpull 10 with e.clear! ?
-
...but I DID manage to get it to reverse faces
model = Sketchup.active_model selection = model.active_entities # - - - - - - - - - - - - selection.each { |entity| if entity.typename == "Face" entity.reverse! end }
Why does reverse! work, but not clear! ?
-
give em some credit chris, i had to type 'selection' as well
-
.clear! will empty an array, it does not erase anyhing from the model. Glad you found the erase! method.
You can also go through and put everything to erase into an array of entities, then use
entities.erase_entities my_array_of_entities
to delete them all in one shot. Apparently some people have ran into bugsplats when erasing entities one at a time like you've done in your example.
Chris
-
...Nevermind. I found in the parent directory; erase! ( How didn't I notice that?)
model = Sketchup.active_model selection = model.active_entities # - - - - - - - - - - - - selection.each { |entity| if entity.typename == "Face" entity.erase! end }
-
@remus said:
give em some credit chris, i had to type 'selection' as well
Heh, I didn't notice that you switched it around to use the selection set instead of all entities. Well done Now make it smart enough to use the selection set if it exists, and if not, use all entities.
Chris
-
It probably got overrun by new posts, so here's my original question about translation
my main issue (which I've been trying to figure out) is how to use the cursor to set the transformation, like the move tool does; click and drag. I thought I saw something on it once, but am still confused.
Your last post, Chris: Ah, and that's easy.
model = Sketchup.active_model if model.selection == true selection = model.selection else selection = model.active_entities end # - - - - - - - - - - - - selection.each { |entity| if entity.typename == "Face" entity.erase! end }
...well, sort of easy. It seems to be buggy, and deletes random faces, if anything is selected.
How do you put all the entities into an array again? -
just learnt an important lesson about using recursion in combination with a messagebox.
model = Sketchup.active_model entities = model.selection entities.each { |e| if e.typename == "Curve" edgnum = e.count_edges UI.messagebox "edgnum.to_s" else UI.messagebox "Please select a curve" end }
10 points if you can spot what'll happen (without running it!)
-
it counts the edges of any selected curve, and displays them as a string, in a message box. If 20 curves were selected, then 20 different message boxes, with the amount of edges in each curve, is shown, one after another each time you click enter.
-
Could well do, but it's worse than that...points still up for grabs.
-
Close enough, it will keep saying 'please select a curve' until you force quite SU, as it keeps going through the model.
-
oh, I forgot to mention that if no curve is selected, it says the message "Please select a curve".
Oh, and I'm pretty sure you wrote part of your code wrong, which could cause... hmmm.... I don't know... O.K., I'm testing it
-
No, just tested it, I think it says it once for every edge in the selected curve/curves.
-
thats interesting, more experimenting is needed!
edit: perhaps in the morning. Its getting a bit late now
-
model = Sketchup.active_model selection = model.selection # - - - - - - - - - - - - selection.each { |entity| if entity.typename == "Face" UI.openURL "http://www.sketchucation.com/forums/scf/" end }
-
Well, this is not the problem in question, but a selection will not contain an entity "Curve" for one thing. It contains edges though, and they might belong to a curve. So you can test if
e.typename == "Edge"
and then if true test ife.curve != false
.(And this gets back to what Adam was bringing up and Jim also talked about in another post. I think instead of testing if each entity is an edge (which is how I still do it), perhaps test each entity if its part of a curve. (I don't know the best code to do that, but its something to think about).)
I think that a selection can only hold DrawingElements (which does not include curves). I'm not sure why though.
Chris
Advertisement