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. self.brl_db.rpp(wing_block_name,
  81. pmin=(self.wing_hole_center_to_center/-2.0, # x
  82. self.wing_width/-2.0, # y
  83. self.depth - self.wing_thickness), # z
  84. pmax=(self.wing_hole_center_to_center/2.0, # x
  85. self.wing_width/2.0, # y
  86. self.depth) # z
  87. )
  88. #start the screw holes at z= (depth - thickness), with height thickness
  89. right_hole_x = self.wing_hole_center_to_center/2.0
  90. left_hole_x = right_hole_x * -1
  91. curves_and_holes = {'left_wing_curve.s':{'brldb_name':None,
  92. 'x':left_hole_x,
  93. 'r':self.wing_width/2.0},
  94. 'right_wing_curve.s':{'brldb_name':None,
  95. 'x':right_hole_x,
  96. 'r':self.wing_width/2.0}
  97. }
  98. for item_name, item in curves_and_holes.iteritems():
  99. item['brldb_name'] = self.get_next_name(item_name)
  100. self.brl_db.rcc(item['brldb_name'],
  101. base=(item['x'],
  102. 0,
  103. self.depth - self.wing_thickness),
  104. height=(0,
  105. 0,
  106. self.wing_thickness),
  107. radius=item['r'])
  108. # Make a region that is the union of these two objects. To accomplish
  109. # this, we don't need anymore to create any linked list of the items ;-).
  110. wings_block_chamfered = self.get_next_name("wings_chamfered.r")
  111. self.brl_db.combination(wings_block_chamfered,
  112. tree=union(curves_and_holes['left_wing_curve.s']['brldb_name'],
  113. curves_and_holes['right_wing_curve.s']['brldb_name'],
  114. wing_block_name)
  115. )
  116. # now subtract the holes away
  117. brl_db.hole(
  118. hole_start=(curves_and_holes['left_wing_curve.s']['x'], 0, 0),
  119. hole_depth=(0, 0, wing_thickness),
  120. hole_radius=self.screw_hole_diameter/2.0,
  121. obj_list=wings_block_chamfered
  122. )
  123. brl_db.hole(
  124. hole_start=(curves_and_holes['right_wing_curve.s']['x'], 0, 0),
  125. hole_depth=(0, 0, wing_thickness),
  126. hole_radius=self.screw_hole_diameter/2.0,
  127. obj_list=wings_block_chamfered
  128. )
  129. return wings_block
  130. # Makes the two screw holes
  131. # Note that you can provide a single combination name or a list in the
  132. # obj_list parameter, it will be handled correctly, all the tedious list
  133. # building is done under the hood:
  134. def shaft(self):
  135. pass
  136. def wires(self):
  137. pass
  138. class aerosol_can_snap_cap(object):
  139. def __init__(self):
  140. #cap_height = 40
  141. self.rim_inner_diamater = 61.1 # may need to change to 61.0mm
  142. self.rim_lip_height = 3
  143. self.rim_trough_height = 2
  144. self.rim_outer_diameter = 63.1
  145. self.rim_outer_top__to_sprayer_bottom = 24.5
  146. self.sprayer_height = 12
  147. self.sprayer_diameter = 10.5
  148. def main(argv):
  149. with wdb.WDB(argv[1], "My Database") as brl_db:
  150. name_tracker = brlcad_name_generator()
  151. motor = _28BYJ_48(brl_db, name_tracker.get_next_name)
  152. # All units in the database file are stored in millimeters. This constrains
  153. # the arguments to the mk_* routines to also be in millimeters.
  154. if __name__ == "__main__":
  155. main(sys.argv)
  156.