Draw shapes in a shape
-
Hi all, I'm really enjoying this forum. Thanks to all contributors.
Ok, so I created a line tool that saves the coordinates of each point clicked. Find the code below:class PVSketch def activate @model = Sketchup.active_model @ents = @model.active_entities @view = @model.active_view result = Sketchup.send_action "viewTop;" @ip = Sketchup;;InputPoint.new @pts = [] @n = 0 end def onLButtonUp(flags,x,y,view) @ip.pick(view,x,y) if not @pts[0] @pts << @ip.position else @pts << @ip.position line = @ents.add_line @pts[@n-1], @pts[@n] end if @pts[0] && @ip.position==@pts[0] && @n != 0 tools = Sketchup.active_model.tools tool = tools.pop_tool end @n = @n + 1 end end click2_cmd = UI;;Command.new("PV Sketch") { Sketchup.active_model.select_tool PVSketch.new } tool_menu = UI.menu "Tools" tool_submenu = tool_menu.add_submenu "DrawLine" tool_submenu.add_separator tool_submenu.add_item click2_cmd
It works perfectly, the next step is that I want to automatically fill the shape that the user drew with the maximum amount of rectangles, let's say 24cmx48cm. Can someone help me with that?
-
You could pick your points in 3d, so the possibilities could get muddied.
However, let's assume 'flatness'...
You need to collect each newly made edge into another array called @edges - in fact doing that gives access to the edges vertices from which you can get a position for each so the @pts is not strictly necessary ? We'll use @edges later...
If you make a new bounding-box object and add all of the points in @pts to it then you have the bounds on these picked points/edges.
From that you can get the extents of the enclosing bounds 'box'.
Make a rectangle face [inside a group] located at bounds.min...
Then you can work out how many faces will fit within the 'box' in x/y.
Iterate these corner-points and add new faces to the group as needed.
These faces might 'straddle' the lines you have drawn.
If this is not to be allowed you have two choices:
One: intersect the face-group with the edge-entities, adding any new edges to the face-group.
Then work out which edges in the group have vertex.positions outside the bounds... and then erase them en mass. You might now have some rectangles that have been trimmed at the original drawn-lines.
Two: intersect the faces in the group one at a time with the @edges-entities, adding the new edges to a new temp-group. If there are no temp-group entities you can keep the face, otherwise it straddles one of your drawn-lines [@edges] so erase the face and any faceless edges it used to use; erase the temp-group. Now you have a set of rectangular-faces that fit within the edges drawn and no overlaps or trimmed faces... -
@tig said:
You could pick your points in 3d, so the possibilities could get muddied.
It's flat, so that's one obstacle out of the way.
@tig said:
You need to collect each newly made edge into another array called @edges
Ok, so instead of connecting points using lines, should I do something like:
edge = Sketchup.active_model.entities.add_line(@pts[@n-1], @pts[@n]) line = edge.line
Sorry to say, but I lost you after that
-
Sorry we muddle you with using edge/line - I always meant 'edge' - never 'line' as in "[point,vector]".
Can you at least work out the bounds of what's been made ?
Then add some grouped faces with it...
Then we can talk again about trimming/not-overlapping them with your edges collection
Advertisement