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

    Ruby script to output outer face loop?

    Scheduled Pinned Locked Moved Developers' Forum
    20 Posts 4 Posters 2.7k Views 4 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.
    • S Offline
      slbaumgartner
      last edited by

      You find the same curve again and again for each Edge associated with it. When you find a curve, you need to use its edges method to get the other Edges in the same Curve and remove them from further consideration. For instance,

      
      require 'sketchup'
       
      model = Sketchup.active_model
      face = model.selection[0]
      used_edges = []
      if (face) 
        loop = face.outer_loop
        edgeuses = loop.edgeuses 
        
        edgeuses.each do|edgeuse|
          edge = edgeuse.edge
          next if used_edges.index edge 
          curve = edge.curve
          if (curve)
            used_edges = used_edges | curve.edges
            if curve.is_a? Sketchup;;ArcCurve
              UI.messagebox "Edge is an Arc Curve"
            else
              UI.messagebox "Edge is a Curve"
            end
          else
              UI.messagebox "Edge is a Line"
          end
        
        end  # end edgeuses each
        
      end
      
      1 Reply Last reply Reply Quote 0
      • E Offline
        emptyvessel
        last edited by

        I think you gentlemen may be giving me too much credit for my knowledge of ruby.
        I am a rank beginner and do not get everything your are saying or showing in your snippets.

        slbaumgartner: your code still displays a arccurve message for all facets of the arc.
        How is "next if used_edges.index edge" suppose to remove edges from consideration?

        tt_su: Your code fails to run at all. "EdgeUses not found"
        Would you be so kind as to explain how each line of your snippet works.

        Regards,

        1 Reply Last reply Reply Quote 0
        • E Offline
          emptyvessel
          last edited by

          Okay, Looked up the 'Uniq' method Now I am even more puzzled.
          The following code is a modified version of tt_su's.
          Uniq appears to be returning the wrong number of array elements.

          
          model = Sketchup.active_model
          face = model.selection[0]
          
          if face
            count = 0
            edges_and_curves = []
            edges_filtered = []
            
            loop = face.outer_loop
            edgeuses = loop.edgeuses  
          
            edgeuses.each do|edgeuse|
              count += 1
              edge = edgeuse.edge
              edges_and_curves << edge.curve || edge
            end
            
            edges_filtered = edges_and_curves.uniq
          
            UI.messagebox(edges_filtered.length)  # Reports 5 Should be 8
            UI.messagebox(count) # Correctly reports 52. (4 sides and 4 arcs * 12 facets)  Correct.
          end
          
          
          1 Reply Last reply Reply Quote 0
          • S Offline
            slbaumgartner
            last edited by

            @emptyvessel said:

            I think you gentlemen may be giving me too much credit for my knowledge of ruby.
            I am a rank beginner and do not get everything your are saying or showing in your snippets.

            slbaumgartner: your code still displays a arccurve message for all facets of the arc.
            How is "next if used_edges.index edge" suppose to remove edges from consideration?

            tt_su: Your code fails to run at all. "EdgeUses not found"
            Would you be so kind as to explain how each line of your snippet works.

            Regards,

            Hmm. I edited my post after I found errors (typed too fast) - did you try the latest version? I drew a rectangle, changed all the corners to arcs, and ran this and it report four Edges and four ArcCurves... When it completes it dumps a whole Array of stuff on the Console, but that is just because Ruby always returns the last thing it evaluated and I didn't bother to stop it.

            If you have a specific example skp, please post it and I'll try to see what went astray.

            Also, it looks to me like TT also typed in haste. I don't think his code is right...for instance edgeuses is undefined!

            Steve

            1 Reply Last reply Reply Quote 0
            • tt_suT Offline
              tt_su
              last edited by

              <span class="syntaxdefault"><br />model </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br />face </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selection</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br /><br /></span><span class="syntaxdefault">if face<br />  edges_and_curves </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[]<br /></span><span class="syntaxdefault">  face</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">outer_loop</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">edgeuses</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each </span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">edgeuse</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault">    edge </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> edgeuse</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">edge<br />    edges_and_curves </span><span class="syntaxkeyword"><<</span><span class="syntaxdefault"> edge</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">curve </span><span class="syntaxkeyword">||</span><span class="syntaxdefault"> edge<br />  </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">  </span><span class="syntaxcomment"># Because we will have added the Curve entities for each edge segment.<br /></span><span class="syntaxdefault">  edges_and_curves</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">uniq</span><span class="syntaxkeyword">!<br /></span><span class="syntaxdefault">end<br /></span>
              
              1 Reply Last reply Reply Quote 0
              • tt_suT Offline
                tt_su
                last edited by

                In deed - I typed in a hurry and made a mess out of it. I made changes in a hurry now as well, but I still haven't checked it properly. Sorry. I'll get back to this next week. Now I need to get to bed as I'm catching a plane early in the morning.

                1 Reply Last reply Reply Quote 0
                • E Offline
                  emptyvessel
                  last edited by

                  Okay I worked on this this weekend and I am ALMOST getting the output I want...

                  
                  'VB.NET Code output
                  gp.AddLine(0.000,10.000,0.000,2.000)
                  gp.AddArc(0.000,8.000,4.000,4.000,0.000,90.000)
                  gp.AddLine(10.000,12.000,2.000,12.000)
                  gp.AddArc(8.000,8.000,4.000,4.000,0.000,90.000)
                  gp.AddLine(12.000,2.000,12.000,10.000)
                  gp.AddArc(8.000,-0.000,4.000,4.000,0.000,90.000)
                  gp.AddLine(2.000,0.000,10.000,-0.000)
                  gp.AddArc(0.000,0.000,4.000,4.000,0.000,90.000)
                  
                  

                  The start and end arc angles all appear to be 0 and 90.
                  The x,y center point is different for each so I know I am getting each arc in the loop.

                  How does this work?

                  My latest conversion code.

                  
                  require 'sketchup'
                  
                  # Task; Convert sketchup face outer loop to VB.NET graphicpath code
                  
                  def ConvertToDotNetLine(edge)
                    # Convert to VB.NET code graphicpath line definition
                    startpt = edge.start.position
                    endpt = edge.end.position
                      
                    s = "gp.AddLine("
                    s += "%.3f" % startpt.x.to_f + ","
                    s += "%.3f" % startpt.y.to_f + ","
                    s += "%.3f" % endpt.x.to_f + ","
                    s += "%.3f" % endpt.y.to_f + ")\n" 
                    return s
                  end
                  
                  def ConvertToDotNetArc(arc)
                    # Convert to VB.NET code graphicpath arc definition
                  
                    x = arc.center[0]-arc.radius # Left of arc circle's bounding box.
                    y = arc.center[1]-arc.radius # Top  of arc circle's bounding box.
                    wt = arc.radius * 2          # width of arc circle's bounding box.
                    ht = wt                      # height of arc circle's bounding box.
                    startang = arc.start_angle
                    endang = arc.end_angle
                  
                    s = "gp.AddArc("
                    s += "%.3f" % x.to_f + ","
                    s += "%.3f" % y.to_f + ","
                    s += "%.3f" % wt.to_f + ","
                    s += "%.3f" % ht.to_f + ","
                    s += "%.3f" % startang.radians + ","
                    s += "%.3f" % endang.radians + ")\n"  
                    return s
                  end
                  
                  model = Sketchup.active_model
                  face = model.selection[0] 
                  
                  if (face) 
                    sout = ""
                    used_edges = []
                    loop = face.outer_loop
                    edgeuses = loop.edgeuses 
                  
                    edgeuses.each do|edgeuse|
                      edge = edgeuse.edge
                      next if used_edges.index edge 
                      curve = edge.curve
                      if (curve)
                        used_edges = used_edges | curve.edges
                        if curve.is_a? Sketchup;;ArcCurve        
                          sout += ConvertToDotNetArc(curve)
                          # UI.messagebox "Edge is an Arc Curve"        
                        else
                          UI.messagebox "Edge is a Curve"
                        end
                      else
                          sout += ConvertToDotNetLine(edge)
                          # UI.messagebox "Edge is a Line"
                      end    
                    end  # end edgeuses each 
                    
                    f = File.open("C;\\aaapath.txt", "w")
                    f.write(sout)  
                    f.close  
                    
                    UI.messagebox(sout)
                  end
                  
                  
                  1 Reply Last reply Reply Quote 0
                  • E Offline
                    emptyvessel
                    last edited by

                    Discovered the Angle_Between() method and I am now calculating the start angle.

                    Things are looking better...
                    Except now I see that an outer loop in Sketchup is not a true path.
                    Endpoints of edges are not necessarily coincident with the start point of the next edge.

                    Is there some method or setting that will force this situation?
                    Otherwise I am looking at recreating every edge and curve in the correct order.

                    This is turning into a much bigger project than I had originally hoped.

                    1 Reply Last reply Reply Quote 0
                    • tt_suT Offline
                      tt_su
                      last edited by

                      Use the Loop and EdgeUse objects that a face will give you:
                      http://www.sketchup.com/intl/en/developer/docs/ourdoc/face.php#loops

                      That will let you traverse the edges and vertices of a face in correct order.

                      1 Reply Last reply Reply Quote 0
                      • E Offline
                        emptyvessel
                        last edited by

                        Thank you Thomas for the reply.

                        So to verify you are saying that the Loops object returns edges in order, but the outer_loop does not. Correct?

                        [Edit]
                        Nope doesn't seem to matter. It is still creating lines flipped around.
                        (see attachment)
                        [/Edit]


                        Output from my ruby.  Drawn in Top view.

                        1 Reply Last reply Reply Quote 0
                        • tt_suT Offline
                          tt_su
                          last edited by

                          face.outer_loop returns a Loop object. Loop object will return vertices in order and it will return EdgeUse objects in order.

                          If you have an Edge and want to figure out it's direction in relationship with a face you must use edge.reversed_in?(face).

                          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