Ooops
November 28, 2020, 02:19:08 AM

Author Topic: [bmx] Stl Export for OpenB3D by TomToad [ 1+ years ago ]  (Read 644 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Stl Export for OpenB3D by TomToad [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Stl Export for OpenB3D
Author : TomToad
Posted : 1+ years ago

Description : This is the BlitzMax/OpenB3d port of <a href="codearcsa0cd.html?code=3271" >Export stl[/url].

Code :
Code: BlitzMax
  1. 'SaveStl() by TomToad
  2. '
  3. 'Usage
  4. ' SaveStl(Filename$,Entity,Children)
  5. '
  6. '       Filename$ - name of the file To be saved
  7. '       Entity - the entity To be saved
  8. '       Children - True, all children will be saved in the file; False - Only the referenced entity will be saved
  9.  
  10. 'v1.2 05/22/16 BMax - OpenB3D version created
  11. 'v1.1 05/22/16 z axis needed To be flipped
  12. 'v1.0 05/22/16 original version
  13. SuperStrict
  14. Import OpenB3d.OpenB3d
  15. Import OpenB3d.B3dglGraphics
  16.  
  17. Global SaveStlTrisCount:Long = 0 'This holds the total number of triangles saved
  18.  
  19. 'Type To hold a 3d vector
  20. Type Vector3D
  21.         Field X:Float
  22.         Field Y:Float
  23.         Field z:Float
  24. End Type
  25.  
  26.  
  27. 'This Function saves the actual triangles.  Your program will Not call this Function.  It is
  28. '       called by SaveStl() And recursively calls itself For each child entity
  29. Function SaveStlTris(Stream:TStream,Entity:TEntity,Children:Int)
  30.         ' If saving children, Then check If the entity has any children.  If no children exist, Then
  31.         ' recursively call this Function with children set To False, otherwise call this Function
  32.         ' For each child entity
  33.         If Children = True Then
  34.                 If CountChildren(Entity) > 0 Then
  35.                         For Local i:Int = 1 To CountChildren(Entity)
  36.                                 SaveStlTris(Stream,GetChild(Entity,i),True)
  37.                         Next
  38.                 End If
  39.                 SaveStlTris(Stream,Entity,False)
  40.         Else
  41.                 'Now To save the actual entity.
  42.                 For Local SurfaceIndex:Int = 1 To CountSurfaces(TMesh(Entity)) 'Go through each surface
  43.                         Local Surface:TSurface = GetSurface(TMesh(Entity),SurfaceIndex)
  44.                         SaveStlTrisCount = SaveStlTrisCount + CountTriangles(Surface) 'Keep track of number of triangles
  45.                         For Local TriangleIndex:Int = 0 Until CountTriangles(Surface) 'go through each triangle on the surface
  46.                                 Local v0:Int = TriangleVertex(Surface,TriangleIndex,0) 'get the vertices of the triangle
  47.                                 Local v1:Int = TriangleVertex(Surface,TriangleIndex,2) ' vertex 1 And 2 are swapped as stl uses a
  48.                                 Local v2:Int = TriangleVertex(Surface,TriangleIndex,1) ' counter-clockwise ordering
  49.                                
  50.                                 'stl doesn't use scale or rotation, so all the vertices must be transformed to
  51.                                 ' world coordinates
  52.                                 Local t0:Vector3D = New Vector3d
  53.                                 TFormPoint(VertexX(surface,v0),VertexY(surface,v0),VertexZ(surface,V0),Entity,Null)
  54.                                 t0.x = TFormedX()
  55.                                 t0.y = TFormedY()
  56.                                 t0.z = -TFormedZ()
  57.  
  58.                                 Local t1:Vector3D = New Vector3d
  59.                                 TFormPoint(VertexX(surface,v1),VertexY(surface,v1),VertexZ(surface,V1),Entity,Null)
  60.                                 t1.x = TFormedX()
  61.                                 t1.y = TFormedY()
  62.                                 t1.z = -TFormedZ()
  63.                                
  64.                                 Local t2:Vector3D = New Vector3d
  65.                                 TFormPoint(VertexX(surface,v2),VertexY(surface,v2),VertexZ(surface,V2),Entity,Null)
  66.                                 t2.x = TFormedX()
  67.                                 t2.y = TFormedY()
  68.                                 t2.z = -TFormedZ()
  69.                                
  70.                                 'Now To Create the surface normal so that the stl file knows which way is out
  71.                                 Local U:Vector3D = New Vector3D
  72.                                 Local V:Vector3D = New Vector3D
  73.                                
  74.                                 U.x = t1.x-t0.x
  75.                                 U.y = t1.y-t0.y
  76.                                 U.z = t1.z-t0.z
  77.                                
  78.                                 V.x = t2.x-t0.x
  79.                                 V.y = t2.y-t0.y
  80.                                 V.z = t2.z-t0.z
  81.                                
  82.                                 Local Normal:Vector3D = New Vector3D
  83.                                 Normal.x = U.y*V.z-U.z*V.y
  84.                                 Normal.y = U.z*V.x-U.x*V.z
  85.                                 Normal.z = U.x*V.y-U.y*V.x
  86.                                
  87.                                 'write the normal To the file
  88.                                 WriteFloat(Stream,Normal.x)
  89.                                 WriteFloat(Stream,Normal.y)
  90.                                 WriteFloat(Stream,Normal.z)
  91.                                
  92.                                 'write the triangle To the file
  93.                                 WriteFloat(Stream,t0.x)
  94.                                 WriteFloat(Stream,t0.y)
  95.                                 WriteFloat(Stream,t0.z)
  96.                                
  97.                                 WriteFloat(Stream,t1.x)
  98.                                 WriteFloat(Stream,t1.y)
  99.                                 WriteFloat(Stream,t1.z)
  100.                                
  101.                                 WriteFloat(Stream,t2.x)
  102.                                 WriteFloat(Stream,t2.y)
  103.                                 WriteFloat(Stream,t2.z)
  104.                                                                
  105.                                 'attribute count.  set To 0
  106.                                 WriteShort(Stream,0)
  107.                         Next
  108.                 Next
  109.         End If
  110. End Function
  111.  
  112. 'Your program will call this Function
  113. 'Filename: Name of the file To be saved
  114. 'Entity: Parent entity To be saved
  115. 'Children: True To aslo save child entities, False To only save parent
  116.  
  117. Function SaveStl(Filename:String,Entity:TEntity,Children:Int = False)
  118.         SaveStlTrisCount = 0 'reset the triangle count To 0
  119.         Local Stream:TStream = WriteFile(Filename)
  120.         For Local i:Int = 1 To 21 '80 Byte header + triangle count
  121.                 WriteInt(Stream,0)
  122.         Next
  123.        
  124.         SaveStlTris(Stream,Entity,Children) 'save the triangles
  125.         SeekStream(Stream,80) 'move To the triangle count positon
  126.         WriteInt(Stream,SaveStlTrisCount) 'write the number of triangles saved
  127.         CloseFile Stream
  128. End Function
  129.  
  130. '--------------------------------------------
  131. '
  132. '  The code below is a sample of using the
  133. '     Function SaveStl()
  134. '
  135. '---------------------------------------------
  136.  
  137. Graphics3D 800,600
  138.  
  139. Local cube:TEntity = CreateCube() 'Create a cube
  140. Local sphere:TEntity = CreateSphere(8,Cube) 'Create a sphere, make cube its parent
  141. ScaleEntity sphere,2,2,2 'scale And move the sphere
  142. PositionEntity sphere,5,0,0
  143.  
  144. SaveStl("cube.stl",Cube,False) 'save the cube, but Not its children
  145.  
  146. SaveStl("sphere.stl",sphere,False) 'save the sphere
  147.  
  148. SaveStl("all.stl",Cube,True) 'save the cube And all its children
  149.  
  150.  
  151. Local camera:TCamera = CreateCamera()
  152. PositionEntity camera,0,0,-10
  153.  
  154. Local light:TLight = CreateLight()
  155. RotateEntity light,45,45,45
  156.  
  157. While Not KeyHit(KEY_ESCAPE) And Not AppTerminate()
  158.         Cls
  159.        
  160.         UpdateWorld
  161.         RenderWorld
  162.         Flip
  163.         If KeyDown(KEY_W) 'w
  164.                 MoveEntity camera,0,0,.2
  165.         End If
  166.         If KeyDown(KEY_S) 's
  167.                 MoveEntity camera,0,0,-.2
  168.         End If
  169.         If KeyDown(KEY_A) 'a
  170.                 TurnEntity camera,0,-1,0
  171.         End If
  172.         If KeyDown(KEY_D) 'd
  173.                 TurnEntity camera,0,1,0
  174.         End If
  175. Wend


Comments :


markcw(Posted 1+ years ago)

 This is cool, thanks Tom!


Cocopino(Posted 11 months ago)

 Pretty cool indeed, stl files open fine with UltimateUnwrap.Are you considering adding brushes/textures?


TomToad(Posted 11 months ago)

 
Code: [Select]
Are you considering adding brushes/textures? No.  Stl does not contain any color/texture information.  It is a format created by 3DSystems for their SLA 3D printers.  It has become the prominent format for most 3D printing and other rapid prototyping technology.If you want a 3D format for on-screen rendering, there are several other formats which are more suited for that purpose.


Cocopino(Posted 11 months ago)

 Ah, did not know that about the format, thanks.


Blitzplotter(Posted 7 months ago)

 Thanks for sharing Tom . [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal