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.
    • tt_suT Offline
      tt_su
      last edited by

      What is the question here? What is it that you're stuck with?

      1 Reply Last reply Reply Quote 0
      • A Offline
        Aerilius
        last edited by

        The best way to create a script is to create line by line on the console. This makes sure that every line has been tested.

        Here are some things I saw:

        • You probably meant
          sout += ConvertToNetLine(line)

        • There is no Sketchup::Line, but Sketchup::Edge.

        • edge.line has only two elements: [Sketchup::Point3d, Sketchup::Vector3d]

        • Ruby is uses consistent case (case sensitive), everything is lower case except of constants:
          E̶l̶s̶i̶f̶ elsif
          R̶e̶t̶u̶r̶n̶ ̶=̶ return

        • the SketchUp API returns coordinates (and other numbers) as "Length" (of the Length class). Those take their units along into the string, and you would get "AddLine(~2.2cm,…)". So you change the type from Length to Float using to_f.

        • if edge.is_a? Sketchup::Edge cs = edge.vertices.map{ |v| p = v.position; [p.x.to_f, p.y.to_f] }.flatten s + ConvertToNetLine(*cs)
          …

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

          Thank you gentlemen for the replies.

          I really want to get into this conversation, but right now I am slammed
          with work.

          My company just upgraded our MRP system and every app I've ever written that leverages the database it is broken.

          I will get back to this question sometime in the near future... perhaps in a week or two.

          Regards,

          Tom

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

            I guess I am confused about straight lines.
            From the API documentation I was under the impression that an Edge could be
            A line, curve, or arccurve.

            How do I determine which is which in an if statement.
            Won't All three be true for "if edge.is_a? Sketchup::Edge"?

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

              An Edge can be part of an ArcCurve or Curve.

              Use Edge.curve to check this, it will return the curve instance if it is and nil if it't not.
              http://www.sketchup.com/intl/en/developer/docs/ourdoc/edge.php#curve

              1 Reply Last reply Reply Quote 0
              • S Offline
                slbaumgartner
                last edited by

                @tt_su said:

                An Edge can be part of an ArcCurve or Curve.

                Use Edge.curve to check this, it will return the curve instance if it is and nil if it't not.
                http://www.sketchup.com/intl/en/developer/docs/ourdoc/edge.php#curve

                The tricky part for Ruby developers is that ArcCurve and Curve do not appear in the Entities collection. That is, you can't detect that they exist by scanning the Entities. You have to test Edges to find them.

                Steve

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

                  True that - they are meta-entities.

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

                    Okay I have a test script that iterates over the outer loop of a simple rectangle with
                    a radius at each corner. What I cannot figure out is how to return just the four lines and four arcs.

                    What I get with the following code is four lines and a hit for every facet in each arc.

                    What am I doing wrong?

                    
                    require 'sketchup'
                     
                    model = Sketchup.active_model
                    face = model.selection[0]
                    
                    if (face) 
                      loop = face.outer_loop
                      edgeuses = loop.edgeuses 
                      
                      edgeuses.each do|edgeuse|
                    
                        edge = edgeuse.edge
                        curve = edge.curve
                        if (curve)
                          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
                    • 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