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

    ComponentDefinition.behavior.snapto does not work for me

    Scheduled Pinned Locked Moved Developers' Forum
    4 Posts 3 Posters 248 Views 3 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
      MarkBergaas
      last edited by

      Hi,

      I am new to Ruby and I am trying to write a script to create a component that will glue to vertical faces and cut an opening. This would be the equivalent of going to the Edit tab in the Components dialog, selecting my component, and setting the "Glue to:" drop down to "Vertical" and checking the "Cut opening" box.

      As far as I can tell from the documentation, what I need to do in the script is set properties on the Behavior object in the ComponentDefinition for my component. I set behavior.cuts_opening to true and behavior.snapto to SnapTo_Vertical.

      When I do this, and then open the Components dialog to view my new component, the "Cut opening" box is checked but the "Glue to:" selection is "None" and, indeed, when I try to glue an instance of my component to a vertical face it does not work. Interesting, though, that if I use puts to inspect the value of the snapto property it is actually 2 (which I believe is the value of SnapTo_Vertical).

      Is there something else I need to do to make these settings effective? I have attached an rb file that demonstrates this problem, and also included the script in the text below:

      Ruby script example

      Code below:

      ` require 'Sketchup'
      def doComponent
      colPoints = [
      Geom::Point3d.new(0, 0, -2),
      Geom::Point3d.new(10, 0, -2),
      Geom::Point3d.new(10, 10, -2),
      Geom::Point3d.new(0, 10, -2) ]

      newFace = Sketchup.active_model.entities.add_face(colPoints[0], colPoints[1], colPoints[2], colPoints[3])

      newFace.pushpull(8, false)
      newGroupEntities = newFace.all_connected

      newGroupEntities.each do |theEntity|
      if theEntity.kind_of?(Sketchup::Face)
      if theEntity.normal == newFace.normal or theEntity.normal == newFace.normal.reverse
      theEntity.material = 0x948B4E
      theEntity.material.alpha = 0.1
      else
      theEntity.material = 0xBDDEE1;
      end
      end
      end

      newGroup = Sketchup.active_model.entities.add_group(newGroupEntities.to_a)

      #add entities for the cut plane
      colPoints.each do |thePoint|
      thePoint.z = 2
      end
      Sketchup.active_model.entities.add_line(colPoints[0], colPoints[1])
      Sketchup.active_model.entities.add_line(colPoints[1], colPoints[2])
      Sketchup.active_model.entities.add_line(colPoints[2], colPoints[3])
      Sketchup.active_model.entities.add_line(colPoints[3], colPoints[0])

      #create the component
      newGroup = Sketchup.active_model.entities.add_group(Sketchup.active_model.entities.to_a)
      newComponent = newGroup.to_component
      newComponent.definition.name = "My Component"
      newDefinition = Sketchup.active_model.definitions[newComponent.definition.name]

      #transform the component's axes so the cut plane entities and the xy axes coincide
      transformation = Geom::Transformation.translation(Geom::Vector3d.new(0, 0, -4))
      newDefinition.entities.transform_entities(transformation, newComponent.definition.entities.to_a)

      #set the component behavior to glue to ("snap to") vertical surfaces and cut an opening
      newDefinition.behavior.snapto = SnapTo_Vertical
      newDefinition.behavior.cuts_opening = true

      puts "newDefinition.behavior.snapto=" + newDefinition.behavior.snapto.to_s
      end

      doComponent`

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

        I found it! I needed to set behavior.is2d to true, also. Now it works like a charm.

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

          Cool, Mark - and welcome to the forum.

          We keep a running list of mistakes and shortcomings in the API documentation here: http://www.sketchucation.com/forums/scf/viewtopic.php?f=180&t=17047

          If this is the case, please add yours to the list.

          Hi

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

            @markbergaas said:

            I found it! I needed to set behavior.is2d to true, also. Now it works like a charm.

            Well spotted !

            TIG

            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