SketchyPhysics – Waarde uitlezen van object
Ik heb op het forum van SketchUcation gevraagd hoe je een waarde kan uitlezen van 0.00 naar 1.00 van een object, dit zijn de scripts, deze kan je kopiëren en plakken in het scriptgedeelte van het object om uit te lezen (met dank aan Anton_s)
Uitlezen van hinge, servo, motor en corkscrew
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# Script for hinge, servo, motor, and corkscrew. onStart { # Add 2D text, just to output info setVar('text_v_shift', getVar('text_v_shift') + 0.04) @text = Sketchup.active_model.add_note('', 0.01, getVar('text_v_shift')) # Find connected joint @joint_parent = nil @joint = nil @joint_name = nil parent_joints = this.group.get_attribute("SPOBJ", "parentJoints").to_a parent_joints.each { |jname| if jname =~ /hinge|servo|motor|corkscrew/i @joint_name = jname break end } if @joint_name # Find joint group associated with joint name Sketchup.active_model.entities.each { |e| next unless e.is_a?(Sketchup::Group) || e.is_a?(Sketchup::ComponentInstance) if e.get_attribute('SPJOINT', 'name', '') == @joint_name @joint = e else found = false e.entities.each { |ec| if ec.is_a?(Sketchup::Group) && ec.get_attribute('SPJOINT', 'name', '') == @joint_name @joint_parent = e @joint = ec found = true break end } break if found end } end if @joint # Get min/max data associated with the joint min = @joint.get_attribute('SPJOINT', 'min', 0).to_f max = @joint.get_attribute('SPJOINT', 'max', 0).to_f @range = (max - min).degrees # Calculate starting angle if @joint_parent tra = @joint_parent.transformation * @joint.transformation else tra = @joint.transformation end @init_dir = tra.xaxis.transform(this.group.transformation.inverse) @init_angle = min.degrees end } onUpdate { if @joint # Calculate object angle and ratio if @joint_parent tra = @joint_parent.transformation * @joint.transformation else tra = @joint.transformation end dir = @init_dir.transform(this.group.transformation).transform(tra.inverse).normalize # Get theta from -PI to PI theta = -Math.acos(dir.x) theta = -theta if dir.y < 0 ratio = @range.zero? ? 0 : (theta - @init_angle).to_f / @range # Process info here... # Display info @text.text = "#{@joint_name.capitalize} Info - Angle: #{sprintf("%.2f", theta.radians)} deg. Ratio (Controller): #{sprintf("%.2f", ratio)}" end } onEnd { # Remove added text @text.erase! if @text.valid? } |
Uitlezen van slider, piston, spring en corkscrew
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# Script for slider, piston, spring, and corkscrew. onStart { # Add 2D text, just to output info setVar('text_v_shift', getVar('text_v_shift') + 0.04) @text = Sketchup.active_model.add_note('', 0.01, getVar('text_v_shift')) # Find connected joint @joint_parent = nil @joint = nil @joint_name = nil parent_joints = this.group.get_attribute("SPOBJ", "parentJoints").to_a parent_joints.each { |jname| if jname =~ /slider|piston|spring|corkscrew/i @joint_name = jname break end } if @joint_name # Find joint group associated with joint name Sketchup.active_model.entities.each { |e| next unless e.is_a?(Sketchup::Group) || e.is_a?(Sketchup::ComponentInstance) if e.get_attribute('SPJOINT', 'name', '') == @joint_name @joint = e else found = false e.entities.each { |ec| if ec.is_a?(Sketchup::Group) && ec.get_attribute('SPJOINT', 'name', '') == @joint_name @joint_parent = e @joint = ec found = true break end } break if found end } end if @joint # Get min/max data associated with the joint min = @joint.get_attribute('SPJOINT', 'min', 0).to_f max = @joint.get_attribute('SPJOINT', 'max', 0).to_f @range = (max - min).to_f # Calculate starting position pt = this.group.transformation.origin if @joint_parent tra = @joint_parent.transformation * @joint.transformation else tra = @joint.transformation end pt.transform!(tra.inverse) @init_pos = pt.z + min end } onUpdate { if @joint # Calculate object position and ratio pt = this.group.transformation.origin if @joint_parent tra = @joint_parent.transformation * @joint.transformation else tra = @joint.transformation end pt.transform!(tra.inverse) pos = pt.z - @init_pos ratio = @range.zero? ? 0 : pos / @range # Process info here... # Display info @text.text = "#{@joint_name.capitalize} Info - Position: #{sprintf("%.2f", pos)}\" Ratio (Controller): #{sprintf("%.2f", ratio)}" end } onEnd { # Remove added text @text.erase! if @text.valid? } |