sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Draw shapes in a shape

    Scheduled Pinned Locked Moved Developers' Forum
    4 Posts 2 Posters 142 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M Offline
      michaelazer
      last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        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

        1 Reply Last reply Reply Quote 0
        • M Offline
          michaelazer
          last edited by

          @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 😢

          1 Reply Last reply Reply Quote 0
          • TIGT Offline
            TIG Moderator
            last edited by

            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

            TIG

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Buy SketchPlus
            Buy SUbD
            Buy WrapR
            Buy eBook
            Buy Modelur
            Buy Vertex Tools
            Buy SketchCuisine
            Buy FormFonts

            Advertisement