spacepaste

  1.  
  2. """
  3. This is the python version of wdb_example.c using the python-brlcad module.
  4. usage:
  5. python wdb_example.py output.g
  6. # want to render to file?
  7. rtedge -s 1024 -F output.pix output.g box_n_ball.r
  8. pix-png -s 1024 < output.pix > output.png
  9. wdb_example.c header text:
  10. Create a BRL-CAD geometry database from C code.
  11. Note that this is for writing (creating/appending) a database.
  12. Note that since the values in the database are stored in millimeters, the
  13. arguments to the mk_* routines are constrained to also be in millimeters.
  14. python attribution:
  15. :author: Bryan Bishop <kanzure@gmail.com>
  16. :date: 2013-09-09
  17. :license: BSD
  18. """
  19. # sys has argv
  20. import sys
  21. import random
  22. from brlcad.primitives import union, subtract
  23. import brlcad.wdb as wdb
  24. class brlcad_name_generator(object):
  25. def __init__(self):
  26. self.num_parts_in_use_by_part_name = {}
  27. def get_next_name(self, part_name):
  28. try:
  29. self.num_parts_in_use_by_part_name[part_name]+=1
  30. except KeyError:
  31. self.num_parts_in_use_by_part_name[part_name]=1
  32. name_split = part_name.split('.')
  33. name_prefix = '.'.join(name_split[:-1])
  34. name_suffix = '.'+name_split[-1] if len(name_split)>1 else ''
  35. return '{}{}{}'.format(name_prefix, self.num_parts_in_use_by_part_name[part_name], name_suffix)
  36. class _28BYJ_48(object):
  37. def __init__(self, brl_db, get_next_name_func):
  38. self.brl_db = brl_db
  39. self.get_next_name_func = get_next_name_func
  40. self.final_name = None
  41. # part properties
  42. self.diameter=28
  43. self.depth=19
  44. # mounting wings
  45. self.wing_thickness = 1
  46. self.screw_hole_diameter = 4.2
  47. self.wing_width = 7
  48. self.wing_hole_center_to_center = 35
  49. # shaft
  50. self.body_to_shaft_base = 1.5
  51. self.shaft_tip_to_body = 10
  52. self.shaft_tip_to_keyway_base = 6
  53. self.shaft_keyway_base_to_shaft_base = self.shaft_tip_to_body - self.body_to_shaft_base - self.shaft_tip_to_keyway_base
  54. # create the part sections
  55. body = self.body()
  56. mounting = self.mounting_wings()
  57. self.shaft()
  58. self.final_name = self.get_next_name("COMPLETE.r")
  59. brl_db.combination(wings_block_chamfered,
  60. is_region=True,
  61. tree=union(body,
  62. mounting),
  63. shader="plastic {di=.8 sp=.2}",
  64. rgb_color=(64, 180, 96)
  65. )
  66. def get_next_name(self, sub_component_part_name):
  67. return self.get_next_name_func('_28BYJ_48' + '__' + sub_component_part_name)
  68. def body(self):
  69. main_body = self.get_next_name('main_body.s')
  70. # create the motor body centered at x0,y0,z0
  71. self.brl_db.rcc(main_body,
  72. base=(0, 0, 0),
  73. height=(0, 0, self.depth),
  74. radius=self.diameter/2.0)
  75. return main_body
  76. def mounting_wings(self):
  77. # Make an rpp under the sphere (partly overlapping). Note that this really
  78. # makes an arb8, but gives us a shortcut for specifying the parameters.
  79. wing_block_name = self.get_next_name("mounting_wings_rect.s")
  80. print wing_block_name
  81. self.brl_db.rpp(wing_block_name,
  82. pmin=(self.wing_hole_center_to_center/-2.0, # x
  83. 0, # y
  84. self.depth - self.wing_thickness), # z
  85. pmax=(self.wing_width, # x
  86. self.wing_hole_center_to_center/2.0, # y
  87. self.depth) # z
  88. )
  89. #start the screw holes at z= (depth - thickness), with height thickness
  90. right_hole_x = self.wing_hole_center_to_center/2.0
  91. left_hole_x = right_hole_x * -1
  92. curves_and_holes = {'left_wing_curve.s':{'brldb_name':None,
  93. 'x':left_hole_x,
  94. 'r':self.wing_width/2.0},
  95. 'left_wing_hole.s':{'brldb_name':None,
  96. 'x':left_hole_x,
  97. 'r':self.screw_hole_diameter/2.0},
  98. 'right_wing_curve.s':{'brldb_name':None,
  99. 'x':right_hole_x,
  100. 'r':self.wing_width/2.0},
  101. 'right_wing_hole.s':{'brldb_name':None,
  102. 'x':right_hole_x,
  103. 'r':self.screw_hole_diameter/2.0}}
  104. for item_name, item in curves_and_holes.iteritems():
  105. item['brldb_name'] = self.get_next_name(item_name)
  106. self.brl_db.rcc(item['brldb_name'],
  107. base=(item['x'],
  108. 0,
  109. self.depth - self.wing_thickness),
  110. height=(0,
  111. 0,
  112. self.wing_thickness),
  113. radius=item['r'])
  114. # Make a region that is the union of these two objects. To accomplish
  115. # this, we don't need anymore to create any linked list of the items ;-).
  116. wings_block_left_chamfered = self.get_next_name("wings_left_chamfered.r")
  117. self.brl_db.combination(wings_block_left_chamfered,
  118. is_region=True,
  119. tree=union(curves_and_holes['left_wing_curve.s'],
  120. wing_block_name),
  121. shader="plastic {di=.8 sp=.2}",
  122. rgb_color=(64, 180, 96)
  123. )
  124. wings_block_chamfered = self.get_next_name("wings_chamfered.r")
  125. self.brl_db.combination(wings_block_chamfered,
  126. is_region=True,
  127. tree=union(curves_and_holes['right_wing_curve.s'],
  128. wings_block_left_chamfered),
  129. shader="plastic {di=.8 sp=.2}",
  130. rgb_color=(64, 180, 96)
  131. )
  132. # now subtract the holes away
  133. wings_block_left_hole_subtracted = self.get_next_name("wings_left_subtracted.r")
  134. self.brl_db.combination(wings_block_left_hole_subtracted,
  135. is_region=True,
  136. tree=subtract(wings_block_chamfered,
  137. curves_and_holes['left_wing_hole.s']),
  138. shader="plastic {di=.8 sp=.2}",
  139. rgb_color=(64, 180, 96)
  140. )
  141. wings_block = self.get_next_name("wings_block.r")
  142. self.brl_db.combination(wings_block,
  143. is_region=True,
  144. tree=subtract(wings_block_left_hole_subtracted,
  145. curves_and_holes['right_wing_hole.s']),
  146. shader="plastic {di=.8 sp=.2}",
  147. rgb_color=(64, 180, 96)
  148. )
  149. return wings_block
  150. # Makes the two screw holes
  151. # Note that you can provide a single combination name or a list in the
  152. # obj_list parameter, it will be handled correctly, all the tedious list
  153. # building is done under the hood:
  154. """
  155. brl_db.hole(
  156. hole_start=(0, 0, 0),
  157. hole_depth=(2, 4, 2.5),
  158. hole_radius=0.75,
  159. obj_list="mounting_wings.r"
  160. )
  161. """
  162. def shaft(self):
  163. pass
  164. def wires(self):
  165. pass
  166. class aerosol_can_snap_cap(object):
  167. def __init__(self):
  168. #cap_height = 40
  169. self.rim_inner_diamater = 61.1 # may need to change to 61.0mm
  170. self.rim_lip_height = 3
  171. self.rim_trough_height = 2
  172. self.rim_outer_diameter = 63.1
  173. self.rim_outer_top__to_sprayer_bottom = 24.5
  174. self.sprayer_height = 12
  175. self.sprayer_diameter = 10.5
  176. def main(argv):
  177. with wdb.WDB(argv[1], "My Database") as brl_db:
  178. name_tracker = brlcad_name_generator()
  179. motor = _28BYJ_48(brl_db, name_tracker.get_next_name)
  180. # All units in the database file are stored in millimeters. This constrains
  181. # the arguments to the mk_* routines to also be in millimeters.
  182. if __name__ == "__main__":
  183. main(sys.argv)
  184.