sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    How do I correctly set the Length formulas in a DC via Ruby

    Scheduled Pinned Locked Moved Developers' Forum
    8 Posts 2 Posters 1.1k Views 2 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.
    • JHJ Offline
      JH
      last edited by

      Hello to all !

      After reading a lot of everything that Icould find
      on the net which was related to Dynamic Components and Ruby,
      and a good Hint from Dan Rathburn πŸ‘ , I managed to add
      Lenx, Leny and Lenz to a Component I created with Ruby. πŸ˜„

      But I cannot find out how to set the _lenx_formula,
      _leny_formula and _lenz_formula correctly, which means
      if I manually enter a value in the DC Dialog it works,
      but not if I set the Values from Code thru a simple dialog box. 😠

      Any help would be very appreciated ❗

      Sample code:

      #set the component dictionary
      @Component_dictionary = 'dynamic_attributes'

      First prompt for the dimensions. This is done using the inputbox

      method. In this case, we will actually use a wrapper for UI.inputbox

      that is defined in sketchup.rb which does some extra error checking

      The first step is to create some arrays which contain the prompts

      and default values.

      prompts = [("Bemerkung"),("Artikel"),("Laenge"), ("Breite"),("Staerke"),("Kante_links"),("Kante_rechts"),("Kante_oben"),("Kante_unten"),("Belag_Vorn"),("Belag_hinten")]

      values = ["BodenDecke","Material1", 600.mm, 400.mm, 19.mm,"Kante1", "Kante1","","","",""]

      list = ["", "Material1|Material2|Material3|Material4|Material5|Material6", "", "", "","|Kante1|Kante2|Kante3|Kante4|Kante5|Kante6","|Kante1|Kante2|Kante3|Kante4|Kante5|Kante6","|Kante1|Kante2|Kante3|Kante4|Kante5|Kante6","|Kante1|Kante2|Kante3|Kante4|Kante5|Kante6","|Belag1|Belag2|Belag3|Belag4|Belag5|Belag6","|Belag1|Belag2|Belag3|Belag4|Belag5|Belag6"]

      results = UI.inputbox(prompts, values, list, "Boden")

      lenx_formula=laenge.to_f
      leny_formula=breite.to_f
      lenz_formula=staerke.to_f

      comp1_def.definition.set_attribute @Component_dictionary,'lenx', '_lenx_formula'
      comp1_def.definition.set_attribute @Component_dictionary,'_lenx_label', 'LenX'
      comp1_def.set_attribute @Component_dictionary,'lenx', '_lenx_formula'

      comp1_def.definition.set_attribute @Component_dictionary,'leny','_leny_formula'
      comp1_def.definition.set_attribute @Component_dictionary,'_leny_label', 'LenY'
      comp1_def.set_attribute @Component_dictionary,'leny','_leny_formula'

      comp1_def.definition.set_attribute @Component_dictionary,'lenz', '_lenz_formula'
      comp1_def.definition.set_attribute @Component_dictionary,'_lenz_label', 'LenZ'
      comp1_def.set_attribute @Component_dictionary,'lenz', '_lenz_formula'

      $dc_observers.get_latest_class.redraw_with_undo
      (comp1_def_instance)

      1 Reply Last reply Reply Quote 0
      • J Offline
        Jim
        last edited by

        Not this:

        
         comp1_def.definition.set_attribute @Component_dictionary,'lenx', '_lenx_formula'
        
        

        Correct:

        
         comp1_def.definition.set_attribute(@Component_dictionary, '_leny_formula', '2*LenX')
        
        
        

        Please provide minimal but complete and executable code when asking for help. This means properly indented, properly namespaced, with a menu item, and no Syntax Errors. You will get better help. Also, either attach the .rb file or wrap the posted code in

        
        [code][/code]
        
        

        tags.

        Hi

        1 Reply Last reply Reply Quote 0
        • JHJ Offline
          JH
          last edited by

          Thank you very mouch,

          But this doesnt seem to function properly,
          In the Attribute Editor I get 0 instead of a self updating value


          DC Editor.PNG

          1 Reply Last reply Reply Quote 0
          • J Offline
            Jim
            last edited by

            Try this code.


            jh_dc_testcode.rb

            Hi

            1 Reply Last reply Reply Quote 0
            • JHJ Offline
              JH
              last edited by

              Thank you Jim,

              I think this is a step in the right direction,
              bur with the Test code U posted I get the Values in red #
              still not updating.
              and in the Attribute Inspector it shows some weird errors.


              DC Editor.PNG


              Attributeinspector.PNG

              1 Reply Last reply Reply Quote 0
              • J Offline
                Jim
                last edited by

                What is the value you are using to set the attribute? Print it out to the Ruby Console using the p command.

                From the image,it could be incorrect decimal separator.

                Or you are including the "=" in the formula.

                Or try not incuding the units in the forumula.

                There are "=" sign in the taxt box but the "Toggle Formula View" is not pressed. The "=" should not be there.

                Hi

                1 Reply Last reply Reply Quote 0
                • J Offline
                  Jim
                  last edited by

                  It looks like you need to do this:

                  
                  comp1_def.definition.set_attribute(@component_dictionary, '_lenx_formula', (laenge.to_f*2.54).to_s)
                  
                  

                  But you will need select the conversion factor based on the DC Dialog units. It will either br cm or inches.

                  Hi

                  1 Reply Last reply Reply Quote 0
                  • JHJ Offline
                    JH
                    last edited by

                    Whow! πŸ˜„ πŸ‘ 😎

                    This seems to work,
                    Now I just need to take care of the units.
                    I will post a reply if I get the Script in a usable condition.

                    It would be nice if You could take a look at it then.

                    But I don't know when I'll get enough time for this.
                    Maybe tomorrow, I got 2go and make Dinner, my Girlfriend already has the knife in her hand 😲
                    and I don't want her to use it on me. πŸ˜†

                    EDIT:
                    I kept using the initial approach in setting the Lenx to _lenx_formula since
                    the other way it left me with an unscalable Component, or at best with a scalable component which doesnt show the correct measurements as the corresponding values in the
                    DC Dialog.
                    But Hey, Thank You anyway, maybe someone else has use for the Code for another
                    useful Plugin πŸ˜„ πŸ‘

                    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