sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    How to draw an edge with an angle and distance

    Scheduled Pinned Locked Moved Developers' Forum
    25 Posts 4 Posters 1.3k 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.
    • M Offline
      morci429
      last edited by

      Hi all,

      As i found the best ruby programmers on this forum, i was wondering if anybody can help do this:

      i managed to work out centeroid of a face now i need to fire three arrows from the point vertically with angels of (45, 90, 135) degrees.

      i know that i can make an edge giving two defined points.
      edg = ents.add_edges point1, point2

      But is there any way to draw an edge with a certain angle and a giving length of 500 MM for example??

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        Do you just want to use a Geom::Vector3d to "fire arrows" and get the Geom::Point3d at the end of the vector??

        Or do you really wish to draw edges from the face's centroid to those points at distance D along angle A ??

        P.S.: You can also draw temporary construction lines, if that helps.

        I'm not here much anymore.

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

          yes i want to draw construction lines from centroid for a distance D with angle A

          Any ideas?
          i looked at the links and i still can't see how can i specify an angle

          P.S i don't know the coordinates of the point the arrow going to stop at

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

            Assuming that ents=model.active_entities etc has been set earlier...
            Try something like

            ce=face.bounds.center
            en=ce.offset(Z_AXIS, 500.mm)
            cl90=ents.add_cline(ce, en)### a vertical line
            tr=Geom;;Transformation.rotation(ce, Y_AXIS, 45.degrees)
            en.transform!(tr)
            cl45=ents.add_cline(ce, en)### a 45 deg line
            tr=Geom;;Transformation.rotation(ce, Y_AXIS, -90.degrees)
            en.transform!(tr)
            cl135=ents.add_cline(ce, en)### a 135 deg line
            
            

            You can substitute add_line if you want a 'real edge'...

            TIG

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

              It needs to be transformed to -90 as we have just rotated 'en' 45 and now want it at -45...

              TIG

              1 Reply Last reply Reply Quote 0
              • Dan RathbunD Offline
                Dan Rathbun
                last edited by

                Ok gotcha...

                I'm not here much anymore.

                1 Reply Last reply Reply Quote 0
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by

                  @morci429 said:

                  i looked at the links and i still can't see how can i specify an angle

                  Sorry, I left out the link to Geom::Transformation class,
                  ... and TIG showed how to use it (above,)

                  EDIT. (TIG had it right,) Disregard this:except that the second tranform should be -45.degrees (not -90.degrees,) so you have an angle 135 degrees off the face's plane.

                  @morci429 said:

                  P.S i don't know the coordinates of the point the arrow going to stop at

                  After you transform the end points, you can save the en var and you have the end points, OR you also have vars referencing the new clines.
                  You can get the endpoints using the .end() instance method for Sketchup::ConstructionLine class, ie:
                  ep45 = cl45.end

                  I'm not here much anymore.

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

                    Thank you so much guys I’m starting to feel like you are magicians 😄

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

                      Guys sorry i don't mean to be pushy, put after i fired the arrow from centroid I’m looking for a built in function in either add_cline or even add_line to retrieve all the faces that intersect with my line.

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

                        @morci429 said:

                        Guys sorry i don't mean to be pushy, but after i fired the arrow from centroid I’m looking for a built in function in either add_cline or even add_line to retrieve all the faces that intersect with my line.

                        At each step as you add the 'line' you have two points ' ce' and ' en', the vector ' vec=ce.vector_to(en)' returns the 'arrow'.
                        Now ' dist=ce.distance(en)' will help us keep track of how far we are going from ' ce'
                        New use a 'raytest' to find the intersections...

                        <span class="syntaxdefault">hits</span><span class="syntaxkeyword">=[]<br /></span><span class="syntaxdefault">pe</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">ce</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">clone </span><span class="syntaxcomment">### set up a copy of the point to raytest from<br /></span><span class="syntaxdefault">goon</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">true<br />while goon<br />  rayt</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">raytest</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">pe</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> vec</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">  goon</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">false if not rayt<br />  pt</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">rayt</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault">  hit</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">rayt</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">][</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault">  goon</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">false if ce</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">distance</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">pt</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">></span><span class="syntaxdefault"> dist<br />  hits </span><span class="syntaxkeyword"><<</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[</span><span class="syntaxdefault">pt</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> hit</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault">  pe</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">pt </span><span class="syntaxcomment">### look from last hit point...<br /></span><span class="syntaxdefault">end</span><span class="syntaxcomment">#while<br /></span><span class="syntaxdefault"> </span>
                        

                        ' hits' is an array of arrays of the points and what was hit at each one...
                        Repeat this for each 'ray' you are making.
                        If you want to find ALL intersections forget the ' dist' test...

                        TIG

                        1 Reply Last reply Reply Quote 0
                        • thomthomT Offline
                          thomthom
                          last edited by

                          @dan rathbun said:

                          @TIG: I think hit=rayt[0][0] would return the x coordinate of pt ?

                          Yes.

                          rayt[0] => Geom::Point3d
                          rayt[1] => Array<path of entities>

                          Thomas Thomassen — SketchUp Monkey & Coding addict
                          List of my plugins and link to the CookieWare fund

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

                            You're right - my daft typo... I've corrected the original 😒
                            hit=rayt[1][0]

                            TIG

                            1 Reply Last reply Reply Quote 0
                            • Dan RathbunD Offline
                              Dan Rathbun
                              last edited by

                              When done.. you will need to test the hits array members and retrieve objects that are certain types.

                              <span class="syntaxdefault">hit_faces </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> hits</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">select </span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|</span><span class="syntaxdefault"> e</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">][</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">instance_of</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Face</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">hit_comps </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> hits</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">select&nbsp;</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|</span><span class="syntaxdefault"> e</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">][</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">instance_of</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">ComponentInstance</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">}</span><span class="syntaxdefault"> </span>
                              

                              ..etc ...

                              @TIG: I think hit=rayt[0][0] would return the x coordinate of pt ?

                              I'm not here much anymore.

                              1 Reply Last reply Reply Quote 0
                              • Dan RathbunD Offline
                                Dan Rathbun
                                last edited by

                                Hey you missed my boo-boo I had written hits.each instead of hits.select
                                (corrected the code..)

                                I'm not here much anymore.

                                1 Reply Last reply Reply Quote 0
                                • Dan RathbunD Offline
                                  Dan Rathbun
                                  last edited by

                                  morc ... use [ code=php ] and [ /code ] tags around your sourcecode when you post it here.

                                  I sent you a PrivateMessage, explaining how.

                                  You are missing 2 end statements at the bottom, just before the **}** closing of the block.

                                  I'm not here much anymore.

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

                                    Tig it isn't working.
                                    guys could u have a look at the code and tell me what do you think?

                                    
                                    require 'sketchup.rb'
                                    
                                    UI.menu("PlugIns").add_item("Fire Arrows") {
                                    
                                    ents = Sketchup.active_model.entities
                                    
                                    Sketchup.active_model.selection.each do |e|
                                    if e.is_a? Sketchup;;Face
                                    
                                    ce=e.bounds.center
                                    
                                    en=ce.offset(e.normal, 100000.mm)
                                    
                                    cl90=ents.add_cline(ce, en)   #a vertical line
                                    
                                    tr=Geom;;Transformation.rotation(ce, Z_AXIS, 45.degrees)
                                    en.transform!(tr)
                                    cl45=ents.add_line(ce, en)   #a 45 deg line
                                    
                                    
                                    vec=ce.vector_to(en)
                                    dist=ce.distance(en)
                                    
                                    hits=[]
                                    pt = Geom;;Point3d.new  
                                    pe=ce.clone ### set up a copy of the point to raytest from
                                    goon=true
                                    while goon
                                      rayt= Sketchup.active_model.raytest(pe, vec)
                                      goon=false if not rayt
                                      pt=rayt[0]
                                      hit=rayt[1][0]
                                      goon=false if ce.distance(pt) > dist
                                      hits << [pt, hit]
                                      pe=pt ### look from last hit point...
                                    end#while
                                    
                                    
                                    hits.each do |s|
                                    if s[1][0].is_a? Sketchup;;Face
                                    hit_face = s.bounds
                                    zmin = hit_face.min.z
                                    zmax = hit_face.max.z
                                    h = zmax - zmin
                                    h = h.to_m
                                    UI.messagebox s[1][0]
                                    UI.messagebox h
                                    end
                                    end
                                    
                                    
                                    tr=Geom;;Transformation.rotation(ce, Z_AXIS, -90.degrees)
                                    en.transform!(tr)
                                    cl135=ents.add_cline(ce, en)  #a 135 deg line
                                    
                                    #en=ce.offset(Z_AXIS, 5000.mm)
                                    
                                    #tr=Geom;;Transformation.rotation(ce, X_AXIS, -45.degrees)
                                    #en.transform!(tr)
                                    #cl135=ents.add_cline(ce, en)  #a 135 deg line
                                    
                                    #tr=Geom;;Transformation.rotation(ce, X_AXIS, 90.degrees)
                                    #en.transform!(tr)
                                    #cl135=ents.add_cline(ce, en)  #a 135 deg line
                                    
                                    
                                    
                                    end
                                    end
                                    }
                                    

                                    Thanks Dan,
                                    i added the two end statements. but the complier doesn't like line 31 ]

                                    pt=rayt[0]
                                    

                                    thanks again Dan

                                    1 Reply Last reply Reply Quote 0
                                    • thomthomT Offline
                                      thomthom
                                      last edited by

                                      @morci429 said:

                                      Tig it isn't working.

                                      Error messages?
                                      Unexpected behaviour?

                                      It always help to be specific to what isn't working.

                                      Thomas Thomassen — SketchUp Monkey & Coding addict
                                      List of my plugins and link to the CookieWare fund

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

                                        Error: #<NoMethodError: undefined method []' for nil:NilClass> C:/PROGRA~1/Google/GOOGLE~3/Plugins/Fire Arrows.rb:31 C:/PROGRA~1/Google/GOOGLE~3/Plugins/Fire Arrows.rb:7:in each'
                                        C:/PROGRA~1/Google/GOOGLE~3/Plugins/Fire Arrows.rb:7
                                        C:/PROGRA~1/Google/GOOGLE~3/Plugins/Fire Arrows.rb:3:in `call'

                                        Sorry if i wasn't clear enough

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

                                          Try adding a test after the goon=false if not rayt like
                                          next if not rayt that way it will jump.

                                          I notice that your 'line' creation is a bit muddled - you a make one then transform the end_point [en] BUT doing so around the Z_AXIS leaves it where it is - do it around the X_AXIS or Y_AXIS...

                                          TIG

                                          1 Reply Last reply Reply Quote 0
                                          • Dan RathbunD Offline
                                            Dan Rathbun
                                            last edited by

                                            morc..

                                            EDIT:The problem is that the API doc does not say that if the ray does not hit anything, the raytest method just returns nil.

                                            rayt= Sketchup.active_model.raytest(pe, vec)

                                            If you read the API for Model.raytest(), you'll see that the first argument must be an Array; the second optional argument (added in SU8.0M1,) is the wysiwyg_flag that sets whether to hit hidden geometry.

                                            EDIT:Actually the method is smart enough to know what you mean... you don't really need an Array arg, unless you want to use the wysiwyg_flag %(#BF4000)[argument.

                                            So you can to call it this way:]
                                            rayt= Sketchup.active_model.raytest( **[**pe, vec**]** )

                                            I'm not here much anymore.

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

                                            Advertisement