• Login
sketchucation logo sketchucation
  • Login
ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

Problem with variables assignement

Scheduled Pinned Locked Moved Developers' Forum
10 Posts 2 Posters 448 Views
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.
  • D Offline
    davidsuke
    last edited by 22 Apr 2009, 12:09

    Hi, I'm new in Sketchup and ruby. I'm from Spain, sorry for my english.
    I have the next problem. I want to save in a variable a face and I do it like this

    vec= [] //It is a list of points
    faceIwant = model.active_entities.add_face vec

    //faceIwant is the face i want to save
    f[0] = faceIwant

    But later I use anotther method which do this:
    vec1= [] //It is a list of points
    face = model.active_entities.add_face vec1

    Then, if I watch the variable f[0] i have the "face", not "haveIwant". I don't know why f[0] changes. Someone can help me??

    Thanks!

    1 Reply Last reply Reply Quote 0
    • T Offline
      TIG Moderator
      last edited by 22 Apr 2009, 12:51

      @davidsuke said:

      Hi, I'm new in Sketchup and ruby. I'm from Spain, sorry for my english.
      I have the next problem. I want to save in a variable a face and I do it like this
      vec= [] //It is a list of points
      faceIwant = model.active_entities.add_face vec

      //faceIwant is the face i want to save
      f[0] = faceIwant

      But later I use another method which do this:
      vec1= [] //It is a list of points
      face = model.active_entities.add_face vec1
      Then, if I watch the variable f[0] i have the "face", not "haveIwant". I don't know why f[0] changes. Someone can help me??
      You haven't given all of your coding steps...
      I'd use names that reflect the variable 'vec' suggests 'vectors'... I'd use 'points'...
      Also it's not clear where your 'f' comes from - is it an array (list) of faces if so f[0] is the first one ? Setting f[0]=face overwrites the value of the first item in the list: you must use something like 'push' to add it onto the end of the list as shown below...

      model=Sketchup.active_model; entities=model.active_entities
      points=[] ### empty array (list) for points
      ## add some points to this array (list)
      ## ...
      ## let's assume we now have a list of good coplanar points called 'points'
      faces=[] ### empty array (list) for all of your faces
      ## let's add some faces to this array (list)
      face=entities.add_face(points)
      faces.push(face)
      ## empty the points array (points=[]) and add some new points - as before
      ## ...
      ## make another face and add it onto list of faces (using push)
      face1=entities.add_face(points)
      faces.push(face1)
      ## etc etc
      ## face[0] is face
      ## face[1] is face1
      ## face[2] is face2
      ## etc
      
      

      Hope this helps...

      TIG

      1 Reply Last reply Reply Quote 0
      • D Offline
        davidsuke
        last edited by 22 Apr 2009, 13:53

        Sorry for my strange constrcution of code. Yes, here isn't it all of my code, the next time I'll put all. Otherwise, thanks fo reply!!

        I'll try and I tell you if it's work, because I think this is that I need.

        1 Reply Last reply Reply Quote 0
        • D Offline
          davidsuke
          last edited by 23 Apr 2009, 12:12

          I'l try it and isn't work.

          Look, I have the next code

          
          require 'sketchup'
          
          Sketchup.send_action "showRubyPanel;"
          
          def main1() 
          model = Sketchup.active_model
          $entities = model.active_entities
          $selection = model.selection
          floor = []
            
          pts = []
          pts[0] = [0, 0, 0]
          pts[1] = [100, 0, 0]
          pts[2] = [100, 100, 0]
          pts[3] = [0, 100, 0]
          
          face = $entities.add_face pts
          floor.push(face)
          
          pts1 = []
          pts1[0] = [25, 25, 0]
          pts1[1] = [75, 25, 0]
          pts1[2] = [75, 75, 0]
          pts1[3] = [25, 75, 0]
          
          face1 = $entities.add_face pts1
          
          edgesFloor = floor[0].edges
          status = $selection.add edgesFloor
          end
          
          UI.menu("PlugIns").add_item("Make house Copy") {main1}
          
          

          I want to view in blue(selected) the edges of the firt square ONLY. But the two squares are selected and i don't know the reason because I only do faces.push() on the first square.

          Any solution??

          Thanks,

          1 Reply Last reply Reply Quote 0
          • T Offline
            TIG Moderator
            last edited by 23 Apr 2009, 12:46

            By the way - you don't normally need to make global variables like $selection, doing this can clash with other scripts using the same global-variable ($xxx) - use a class or module and @xxx variables common within that - if you feel you must use $xxx ones give them very unique names unlikely to clash with other's...

            I can now see what's up. When you set faceEdges to be face's edges it quite rightly highlights all of them because the inner loop of edges forming face1 also bound 'face'. You can remove the inner ones by using

            selection.add{face.edges)
            selection.remove(face1.edges)
            

            Note that you don't actually need to make the arrays of faces unless you need them later as 'face' and 'face1' are directly accessible...
            The .edges methods returns all of a face's edges - if you want to to select only the outer edges of a face use

            selection.add(face.outer_loop.edges)
            

            .

            TIG

            1 Reply Last reply Reply Quote 0
            • D Offline
              davidsuke
              last edited by 23 Apr 2009, 13:04

              Thanks! but i don't understan't why when I do

              
              edgesFloor = floor[0].edges
              
              

              why I have all edges of my project inside edgesFloor, because I only put "face" in faces[0], not "face1".
              It's not possible to have the face "face" alone doing faces[0] = face???

              1 Reply Last reply Reply Quote 0
              • T Offline
                TIG Moderator
                last edited by 23 Apr 2009, 15:18

                @davidsuke said:

                Thanks! but i don't understan't why when I do

                
                > edgesFloor = floor[0].edges
                > 
                

                why I have all edges of my project inside edgesFloor, because I only put "face" in faces[0], not "face1".
                It's not possible to have the face "face" alone doing faces[0] = face???

                If you had found the edges to 'face' and put them into an array BEFORE you made 'face1' then that would work OK. However, you don't try to find 'face's' edges until after you have added 'face1'. At that point 'face' and 'face1' are coincident/coplanar in the Z-plane so 'face1' is effectively inserted into the middle of 'face'. 'Face' then has four extra edges - i.e. the internal loop that it shares with face1 ! So you must either find the edges to 'face' before making any other coplanar faces or later on remove the edges for 'face1' from any selection.

                Try making this manually - you'll see that if you make 'face1' on top of 'face', then 'face' will become a doughnut when selected, double-click on it and its face and edges are highlighted - including the inner-loop !

                TIG

                1 Reply Last reply Reply Quote 0
                • D Offline
                  davidsuke
                  last edited by 23 Apr 2009, 17:05

                  ok! I understand now!! You're a master!!!
                  But i hsve the last question for you, sorry lol

                  @unknownuser said:

                  So you must either find the edges to 'face' before making any other coplanar faces or later on remove the edges for 'face1' from any selection.

                  Well, can you tell me how can I have the edges of my "face"after add "face1"???
                  I know the second method, remove the lines of the "face1" from selection, but it's possible take only the edges of "face" directly??

                  1 Reply Last reply Reply Quote 0
                  • T Offline
                    TIG Moderator
                    last edited by 23 Apr 2009, 17:10

                    selection.add(face.outer_loop.edges)
                    

                    will add the outer edges of face and ignore the inner ones at any point in the code after face is made... even after face1 is added...

                    TIG

                    1 Reply Last reply Reply Quote 0
                    • D Offline
                      davidsuke
                      last edited by 23 Apr 2009, 17:21

                      Thanks!!! Now I can continue with my project!!

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

                      Advertisement