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

    Flatten Plane / Match plane

    Scheduled Pinned Locked Moved Plugins
    30 Posts 4 Posters 1.1k 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.
    • thomthomT Offline
      thomthom
      last edited by

      @stevo7122 said:

      Actually the mesh optimizer is what I was wanting to have the ability to preserve the selected edges and remove the interior edges.

      So you are in fact flattening the outer edges? When you say preserve you just want to not erase it - but you can modify it so everything becomes planar?
      It's really help if you posted some visual examples.

      @stevo7122 said:

      Which vertex tools are you referring to? Are those available as a plugin?

      http://www.thomthom.net/software/vertex_tools/

      @stevo7122 said:

      Also how are you storing face1 and face2 in your example. The one thing I can't figure out is how to refrence the selected face. I know you can set selection = sketchup.active_model.selection but how do you get the currently sellected face from that?

      
      # Assuming only one face is selected;
      sel = Sketchup.active_model.selection
      face1 = sel[0]
      
      
      
      # Assuming only two faces are selected;
      sel = Sketchup.active_model.selection
      face1, face2 = sel.to_a
      
      

      Sketchup.active_model.selection is an Enumerable object.

      Sketchup.active_model.selection.to_a will give you an Array.

      I take it you are quite new to scripting? Are you familiar with Enumerable and Array?

      @stevo7122 said:

      I know how to test to see what is selected by checking the entity.typename == "face" but how do I store the selected face in a a variable I.E. face1 = currently selected face...

      #typename is horribly slow. Use #is_a? instead. Have a look at this article for more info: http://www.thomthom.net/thoughts/2011/12/never-ever-use-typename/

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

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

        I think I figured it out. I actually needed to set n0=selection[1].normal I.E. the normal of the other plane...One last quick question is their a way to remove the conjoined edge between the two triangles after I make them coplanar? Im sure in the api you can find the conjoined edge is their a delete function that you would perform?Thank you both for all of your help.

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

          After you are sure they are now coplanar... and ignoring that they might have different materials etc... try something like...
          es0=face0.edges; face1.edges.each{|e|e.erase! if e.valid? && es0.include?(e)}

          TIG

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

            thomthom I just purchased your vertex tools and will give them a try. I do have one last issue. It seems that no matter which order I select to flatten the two triangles it always flattens to the plane of the same triangle. What i'm wanting is to flatten to the plane of the first selected triangle depending on which order I select. Using Tig's code below I removed the z axis and set it equal to the normal of the first plane:
            ` #Set m = to the active Model
            m=Sketchup.active_model;
            m.start_operation('f');

            #Get the first plane and store it in f
            f=m.selection[0];
            #n0=Z_AXIS.clone;

            #Get the normal of the second plane and store it in n0
            n0=m.selection[1].normal;

            #set n1 = to the first planes normal.
            n1=f.normal;

            #Set c equal to the cross vector of the first plane.
            c=n0.cross(n1);

            set a equal to the angle between the first and second faces.

            a=n0.angle_between(n1);

            t=Geom::Transformation.rotation(f.vertices[0].position,c,-a);
            m.active_entities.transform_entities(t,f);
            m.commit_operation`

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

              Shouldn't this work given that f is equal to the first plane selected or am i not understanding something?

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

                You've swapped the faces' normals over.
                Redo the code more logically
                f0=selection[0] f1=selection[1] n0=f0.normal n1=f1.normal
                etc
                The cross, angle, transformation etc should then go smoothly...
                Then you won't get so confused.
                Incidentally, the selected objects are 'faces' NOT 'planes'...
                You can only have 2 faces selected, for this to be foolproof...

                TIG

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

                  I think my issue is i'm not actually capturing the first triangle being clicked anywhere I'm just enumerating the selected model and incidently just reversing the code. I think I need to some how determine which triangle was clicked first I.E. f0 or f1 and then adjust accordingly.

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

                    Technicallly the first face clicked should be f0=m.selection[0] If I were to select two faces...correct?

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

                      **selection=Sketchup.active_model.selection** f0=selection[0] f1=selection[1] n0=f0.normal n1=f1.normal
                      etc etc

                      With selections, arrays etc the first element is index 0, second index 1 etc...
                      So when you select two faces.
                      The selection has two elements - [0] and [1]
                      These are the faces referred to as f0 and f1 above...
                      From these we get their 'normals' [vectors - drawn perpendicular from their 'planes'] - n0 and n1...

                      That's also why you can't Select any other things and hope this'll work, as there's no code in there [at the moment] to 'extract' the just the two faces from everything else that you might have selected... but then that's the subject for a separate thread... πŸ˜‰

                      TIG

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

                        Right. However in the array will the first position = the first item clicked? For example if I select one face the first array position[0] should contain that face. And in another instance if i click on the first face then the second face will the position[0] still be the first face I clicked on or will the second face be in that position as it changes how the model is enumerated? For some reason when I use the code no matter which face I click on it always flattens the same face regardless of the first face clicked. Currently as it stands if I click the first face and then the second face it will always flatten the first face to the second face....Basically to test the code I will select 1 face then select the next face and it will flatten. However If I perform an undo to reset the faces to the original position and select a different face as the first one it will still flatten the same face regardless of which order I clicked on.
                        ` f0=m.selection[0]; #first face selected
                        f1=m.selection[1]; #second face selected

                        n0=f0.normal; #first plane normal
                        n1=f1.normal; #second plane normal

                        c=n0.cross(n1);

                        set a equal to the angle between the first and second faces.

                        a=n0.angle_between(n1);

                        t=Geom::Transformation.rotation(f0.vertices[0].position,c,-a);
                        m.active_entities.transform_entities(t,f1);
                        m.commit_operation;`

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

                          As an edit I just realized I think my comments should be #first face normal and #second face normal not #first plane normal etc...

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

                            I have been doing some debugging of sorts and if you do a puts f0.normal and a puts f1.normal then select face1 then face2 (look at the two values and the order they are outputted) then perform an undo and select face2 then face1 it produces the same values in the same order. Shouldn't the order be reversed if I clicked on a different plane first in each instance?

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

                              Sorry I meant to say clicked on a different face first...For some reason I keep wanting to say plane..

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

                                edge.erase!

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

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

                                  To get the faces in a specific order you really need to make a Tool class - then pick the faces after the tool is activated, that way the first picked face is set.
                                  We are only using the 'selection' based process to simplify things, as you try to get your head around how to find normals, crosses and transform vertices etc...
                                  The order objects appear in a selection is not necessarily determined by the initial selection order, it may well relate to their 'creation' order - it's arbitrary.
                                  If you recall my initial example even excluded a 'second' face as the selected face was laid 'flat'...

                                  If you need a Tool class to select the faces there are many examples available - find an 'unfold' tool's script...
                                  That will have many details of selecting faces and getting suitable data from them and neighbors etc, used to 'unfold' them - much like you want to do...

                                  TIG

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

                                    Thanks for the response I suspected something like that was going on. I do have code to make sure that only 2 faces have been selected and no other parts of the model. I will look into using the tool class so I can get the correct order of faces. Again Thanks to both of you for all of your help.

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

                                      If you're new to SketchUp plugin development you might want to have a look at this article: http://www.thomthom.net/thoughts/2012/01/golden-rules-of-sketchup-plugin-development/

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

                                      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