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. print 'Body DONE: {}'.format(body)
  57. mounting = self.mounting_wings()
  58. print 'Mounting wings DONE: {}'.format(mounting)
  59. self.shaft()
  60. self.final_name = self.get_next_name("COMPLETE.g")
  61. print 'final motor name: {}'.format(self.final_name)
  62. brl_db.combination(self.final_name,
  63. tree=union(body,
  64. mounting),
  65. )
  66. print 'final motor DONE: {}'.format(self.final_name)
  67. def get_next_name(self, sub_component_part_name):
  68. return self.get_next_name_func('_28BYJ_48' + '__' + sub_component_part_name)
  69. def body(self):
  70. main_body = self.get_next_name('main_body.s')
  71. # create the motor body centered at x0,y0,z0
  72. self.brl_db.rcc(main_body,
  73. base=(0, 0, 0),
  74. height=(0, 0, self.depth),
  75. radius=self.diameter/2.0)
  76. return main_body
  77. def mounting_wings(self):
  78. # Make an rpp under the sphere (partly overlapping). Note that this really
  79. # makes an arb8, but gives us a shortcut for specifying the parameters.
  80. wing_block_name = self.get_next_name("mounting_wings_rect.s")
  81. self.brl_db.rpp(wing_block_name,
  82. pmin=(self.wing_hole_center_to_center/-2.0, # x
  83. self.wing_width/-2.0, # y
  84. self.depth - self.wing_thickness), # z
  85. pmax=(self.wing_hole_center_to_center/2.0, # x
  86. self.wing_width/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. print 1
  115. # Make a region that is the union of these two objects. To accomplish
  116. # this, we don't need anymore to create any linked list of the items ;-).
  117. wings_block_chamfered = self.get_next_name("wings_chamfered.r")
  118. self.brl_db.combination(wings_block_chamfered,
  119. tree=union(curves_and_holes['left_wing_curve.s']['brldb_name'],
  120. curves_and_holes['right_wing_curve.s']['brldb_name'],
  121. wing_block_name)
  122. )
  123. print '2 name: {}'.format(wings_block_chamfered)
  124. # now subtract the holes away
  125. wings_block_left_hole_subtracted = self.get_next_name("wings_left_subtracted.r")
  126. self.brl_db.combination(wings_block_left_hole_subtracted,
  127. tree=subtract(wings_block_chamfered,
  128. curves_and_holes['left_wing_hole.s']['brldb_name'])
  129. )
  130. print '3 name: {}'.format(wings_block_left_hole_subtracted)
  131. wings_block = self.get_next_name("wings_block.r")
  132. self.brl_db.combination(wings_block,
  133. tree=subtract(wings_block_left_hole_subtracted,
  134. curves_and_holes['right_wing_hole.s']['brldb_name'])
  135. )
  136. print '4 name: {}'.format(wings_block)
  137. return wings_block
  138. # Makes the two screw holes
  139. # Note that you can provide a single combination name or a list in the
  140. # obj_list parameter, it will be handled correctly, all the tedious list
  141. # building is done under the hood:
  142. """
  143. brl_db.hole(
  144. hole_start=(0, 0, 0),
  145. hole_depth=(2, 4, 2.5),
  146. hole_radius=0.75,
  147. obj_list="mounting_wings.r"
  148. )
  149. """
  150. def shaft(self):
  151. pass
  152. def wires(self):
  153. pass
  154. class aerosol_can_snap_cap(object):
  155. def __init__(self):
  156. #cap_height = 40
  157. self.rim_inner_diamater = 61.1 # may need to change to 61.0mm
  158. self.rim_lip_height = 3
  159. self.rim_trough_height = 2
  160. self.rim_outer_diameter = 63.1
  161. self.rim_outer_top__to_sprayer_bottom = 24.5
  162. self.sprayer_height = 12
  163. self.sprayer_diameter = 10.5
  164. def main(argv):
  165. with wdb.WDB(argv[1], "My Database") as brl_db:
  166. name_tracker = brlcad_name_generator()
  167. motor = _28BYJ_48(brl_db, name_tracker.get_next_name)
  168. # All units in the database file are stored in millimeters. This constrains
  169. # the arguments to the mk_* routines to also be in millimeters.
  170. if __name__ == "__main__":
  171. main(sys.argv)
  172.