Parametric component

You MUST put your code within a unique namespace module.
Then put your extension code within a extension submodule of your namespace module.

IE …

Do not define methods in the top level ObjectSpace.
Do not define instance variables in the top level ObjectSpace.


Please use 2 space indentation for Ruby code.
Use proper indentation.

Ie … not:

def variables
prompts = nil

prompts = [
	'Epaisseur bardage:', 
	'Profondeur total ebrasement:', 
	'largeur prise:',
	#'Hauteur ebrasement'
	]
# ...

Instead do …

  def variables
    prompts = nil

    prompts = [
      'Epaisseur bardage:', 
      'Profondeur total ebrasement:', 
      'largeur prise:',
      #'Hauteur ebrasement'
    ]
     # ...
  end

Meaning that the opening and closing of block must line up on the left.
Most code editors show indent lines to help with indentation.


Your menu item commands should be in a method because the proc cannot be redefined if you make changes and reload the rb file. Because reloading happens often during development, the menu building should be within a load guard block …

# encdoing: UTF-8

module DecherfBuilder # <<-- namespace for your company
  module MyExtension # <<-- give a better extension name

    extend self

    def variables
      prompts = nil

      prompts = [
        'Epaisseur bardage:', 
        'Profondeur total ebrasement:', 
        'largeur prise:',
        #'Hauteur ebrasement'
      ]

      parametres = [
        20.mm, 
        50.mm, 
        30.mm, 
        #1000.mm
      ]

      parametres = UI.inputbox(prompts, parametres, "Caractéristiques")
      # ALWAYS need to check here if user canceled inputbox:
      return false unless parametres

      # Create some variables
      @epaiss_bard = parametres[0]		#Epaisseur du bardage
      @prof_ebrasement = parametres[1]	#Profondeur ebrasement
      @larg_prise = parametres[2] 		#largeur de prise derrière le bardage
      @hauteur_ebrase = parametres[3]		#hauteur tableau

      return true
    end ### variables()

    def joue_gauche

      # Create the component definition
      model = Sketchup.active_model

      joue_gauche_def = model.definitions.add("joue_gauche")
      joue_gauche_ent = joue_gauche_def.entities

      @rayon_inter = 1.5.mm
      @rayon_exter = 3.mm
      @epaiss_tole = 1.5.mm


      x0 = 0
      x1 = @larg_prise
      x2 = @larg_prise + @rayon_inter
      x3 = x2 + @epaiss_tole
      x4 = x3 + @rayon_inter
      x5 = x4 + @rayon_inter
      x6 = x5 + @epaiss_tole
      y1 = 0 - @epaiss_bard - @rayon_inter
      y2 = 0 - @rayon_inter
      y3 = 0
      y4 = @epaiss_tole
      y5 = @epaiss_tole + @prof_ebrasement - @epaiss_bard
      z = 0

      pt1 = [x0, y3, z]

      # Create a 1/4 circle, radius of 5, normal to the Z axis
      center = Geom::Point3d.new x1, y2, 0
      normal = Geom::Vector3d.new 0, 0, 1
      xaxis = Geom::Vector3d.new 1, 0, 0
      start_a = Math::PI/2
      end_a = 0
      model = Sketchup.active_model
      entities = model.entities
      edgearray1 = entities.add_arc center, xaxis, normal, @rayon_inter, start_a, end_a
      edge = edgearray1[0]
      arccurve = edge.curve
      start_angle1 = arccurve.start_angle

      pt2 = edgearray1[0].start
      pt3 = edgearray1[-1].end

      # Create a 1/4 circle, radius of 5, normal to the Z axis
      center = Geom::Point3d.new x4, y1, 0
      normal = Geom::Vector3d.new 0, 0, -1
      xaxis = Geom::Vector3d.new 1, 0, 0
      start_a = 0
      end_a = Math::PI
      model = Sketchup.active_model
      entities = model.entities
      edgearray2 = entities.add_arc center, xaxis, normal, @rayon_exter, start_a, end_a
      edge = edgearray2[0]
      arccurve = edge.curve
      start_angle2 = arccurve.start_angle

      pt4 = edgearray2[-1].end
      pt5 = edgearray2[0].start #good
      pt6 = [x6, y5, z]
      pt7 = [x5, y5, z]

      # Create a 1/4 circle, radius of 5, normal to the Z axis
      center = Geom::Point3d.new x4, y1, 0
      normal = Geom::Vector3d.new 0, 0, -1
      xaxis = Geom::Vector3d.new 1, 0, 0
      start_a = 0
      end_a = Math::PI
      model = Sketchup.active_model
      entities = model.entities
      edgearray3 = entities.add_arc center, xaxis, normal, @rayon_inter, start_a, end_a
      edge = edgearray3[0]
      arccurve = edge.curve
      start_angle3 = arccurve.start_angle

      pt8 = edgearray3[0].start #good
      pt9 = edgearray3[-1].end

      # Create a 1/4 circle, radius of 5, normal to the Z axis
      center = Geom::Point3d.new x1, y2, 0
      normal = Geom::Vector3d.new 0, 0, 1
      xaxis = Geom::Vector3d.new 1, 0, 0
      start_a = 0
      end_a = Math::PI/2
      model = Sketchup.active_model
      entities = model.entities
      edgearray4 = entities.add_arc center, xaxis, normal, @rayon_exter, start_a, end_a
      edge = edgearray4[0]
      arccurve = edge.curve
      start_angle4 = arccurve.start_angle

      pt10 = edgearray4[0].start
      pt11 = edgearray4[-1].end
      pt12 = [x0, y4, z]

      # Call methods on the Entities collection top drax stuff

      face1 = joue_gauche_ent.add_face pt1, pt2, edgearray1, pt3, pt4, edgearray2, pt6, pt7, edgearray3, edgearray4, pt12

        #atempt to weld the point and the curves
        edges = face1.selection.grep(Sketchup::Edge)
        curves = model.active_entities.weld(edges)

      # entities objects is an edge.
      entity1 = joue_gauche_ent[1]
      # Getting zero.
      number = entity1.find_faces

      x = y = z = 0

      ents = model.active_entities
      joue_gauche1 = ents.add_instance(joue_gauche_def, [x, y, z])
      joue_gauche1.explode

    end ### joue_gauche()

    def joue_gauche_command
      model = Sketchup.active_model
      model.start_operation("Joue gauche")
        # Create a new layer
        l_new = model.layers.add("Bardage")
        model.active_layer = l_new
        # List of entities of the roof
        return unless variables() # bail out if user canceled
        joue_gauche()
      # Commit the operation
      model.commit_operation
    end

    if !defined?(@menu_loaded)
      # Add a menu item to Launch the "Joue gauche" command:
      UI.menu("PlugIns").add_item("Joue gauche") {
        joue_gauche_command()
      }
      @menu_loaded = true
    end

  end # extension submodule
end # top level namespace module

(…scroll to see all code…)


You should never change the active layer without changing it back to “Layer0” when done.
ALWAYS create primitives (edges, faces, and curves) associated with “Layer0”.
Guidepoints, Guidelines, Groups and component instances, and other complex objects like section planes and dimensions can be associated with other custom layers.


Please use parenthesis around the arguments for instance method calls.


Lastly, your joue_gauche() method is WAY TOO LONG. It should be broken up into smaller methods.
(I will not check all that method for errors.)


4 Likes