Advertisement
Guest User

Untitled

a guest
Jan 8th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. Dim swApp              As Object
  4. Dim Part               As Object
  5. Dim skSegment          As Object
  6. Dim boolstatus         As Boolean
  7. Dim longstatus         As Long
  8.  
  9. Dim tabx(1000)         As Double
  10. Dim taby(1000)         As Double
  11. Dim tabz(1000)         As Double
  12.  
  13. Dim length             As Double
  14. Dim boole              As Boolean
  15. Dim n                  As Integer
  16. Dim radius             As Double
  17. Dim securityradius     As Double
  18. Dim dist               As Double
  19.  
  20. Dim MatrixVolume       As Integer
  21. Dim FibersVolume       As Integer
  22. Dim MatrixVolumeFract  As Double
  23. Dim FibersVolumeFract  As Double
  24. Dim MatrixVolumeFractP As Integer
  25. Dim FibersVolumeFractP As Integer
  26. Dim EffectiveTC        As Double
  27. Dim err As Long
  28. Dim war As Long
  29. Dim bool As Boolean
  30. Dim name As String
  31. Dim swmodel As Object
  32.  
  33. Private Sub LengthTextBox_Enter()
  34.  
  35. 'Delete indication text
  36. If LengthTextBox.Value = "Length of RVE" Then
  37. LengthTextBox.Value = ""
  38. End If
  39.  
  40. End Sub
  41.  
  42. Private Sub RadiusTextBox_Enter()
  43.  
  44. 'Delete indication text
  45. If RadiusTextBox.Value = "Radius of inclusions" Then
  46. RadiusTextBox.Value = ""
  47. End If
  48.  
  49. End Sub
  50.  
  51. Private Sub NumbersTextBox_Enter()
  52.  
  53. 'Delete indication text
  54. If NumbersTextBox.Value = "Numbers of inclusions" Then
  55. NumbersTextBox.Value = ""
  56. End If
  57.  
  58. End Sub
  59.  
  60. Private Sub CalculateButton_Click()
  61.  
  62. If LengthTextBox.Value = "Length of RVE" Or LengthTextBox.Value = "" Or RadiusTextBox.Value = "Radius of inclusions" Or RadiusTextBox.Value = "" Or NumbersTextBox.Value = "Numbers of inclusions" Or NumbersTextBox.Value = "" Then
  63. MsgBox "Please fill all the fields !", vbCritical, "Error"
  64. Else
  65.  
  66. 'Show calculate zone
  67. UserForm1.Height = 489.75
  68.  
  69. 'Calculate volume fraction
  70. MatrixVolume = LengthTextBox.Value * LengthTextBox.Value * LengthTextBox.Value
  71. FibersVolume = (4 * Atn(1)) * (RadiusTextBox.Value ^ 2) * LengthTextBox.Value * NumbersTextBox.Value
  72.  
  73. FibersVolumeFract = FibersVolume / MatrixVolume
  74. MatrixVolumeFract = 1 - FibersVolumeFract
  75.  
  76. MatrixVolumeFractP = MatrixVolumeFract * 100
  77. FibersVolumeFractP = FibersVolumeFract * 100
  78.  
  79. MatrixV.Caption = MatrixVolume
  80. FibersV.Caption = FibersVolume
  81. MatrixVF.Caption = MatrixVolumeFractP
  82. FibersVF.Caption = FibersVolumeFractP
  83.  
  84. 'Calculate decimal thermal conductivity and round at 2
  85. EffectiveTC = (TCF.Value * FibersVolumeFract) + (TCM.Value * MatrixVolumeFract)
  86. ETC.Caption = Int(EffectiveTC * 10 ^ 2 + 1 / 2) / 10 ^ 2
  87.  
  88. End If
  89.  
  90. End Sub
  91.  
  92. Private Sub GenerateButton_Click()
  93.  
  94. 'Enregistrement
  95. 'identifier le chemin du dossier
  96. chemin_dossier = "C:\Users\Kader\Desktop\test123"
  97. 'vérifier l'existence du dossier et le créer
  98. If Dir(chemin_dossier, vbDirectory) <> vbNullString Then
  99.     'dossier exuste
  100.    'ne rien faire
  101. Else
  102.     'dossier n'existe pas, le créer
  103.    MkDir (chemin_dossier)
  104. End If
  105.  
  106. If LengthTextBox.Value = "Length of RVE" Or LengthTextBox.Value = "" Or RadiusTextBox.Value = "Radius of inclusions" Or RadiusTextBox.Value = "" Or NumbersTextBox.Value = "Numbers of inclusions" Or NumbersTextBox.Value = "" Then
  107. MsgBox "Please fill all the fields !", vbCritical, "Error"
  108. Else
  109.  
  110. 'open new part
  111. Set swApp = Application.SldWorks
  112. Set Part = swApp.NewDocument("C:\ProgramData\SOLIDWORKS\SOLIDWORKS 2018\templates\Pièce.prtdot", 0, 0, 0)
  113.  
  114. '3D sketch
  115. Part.SketchManager.Insert3DSketch (True)
  116.  
  117. 'number of cylinder
  118. n = NumbersTextBox.Value
  119.  
  120. 'length and radius
  121. length = LengthTextBox.Value / 1000
  122. radius = RadiusTextBox.Value / 1000
  123. securityradius = radius * 1.2
  124.  
  125. 'random generate and no overlap
  126. Dim i, j As Integer
  127. For i = 1 To n
  128. Randomize Timer
  129. tabx(i) = Rnd * (length - securityradius * 2) + securityradius  'x position
  130. taby(i) = Rnd * (length - securityradius * 2) + securityradius  'y position
  131. boole = 0
  132. If (i <> 1) Then
  133. For j = 1 To i - 1
  134. dist = Sqrt((tabx(i) - tabx(j)) ^ 2 + (taby(i) - taby(j)) ^ 2)
  135. If (dist < 2.2 * radius) Then boole = 1
  136. Next j
  137. End If
  138.  
  139. 'add point
  140. If (boole = 0) Then
  141. Dim skPoint As Object
  142. Set skPoint = Part.SketchManager.CreatePoint(tabx(i), taby(i), tabz(i))
  143. 'MsgBox tabx(i) & " " & taby(i) & " " & tabz(i)
  144. Else
  145. i = i - 1
  146. End If
  147. Next i
  148.  
  149. 'circle insertion
  150. Set Part = swApp.ActiveDoc
  151. Part.SketchManager.InsertSketch True
  152. boolstatus = Part.Extension.SelectByID2("Plan de face", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
  153. Part.ClearSelection2 True
  154. Dim skSegment As Object
  155. Set skSegment = Part.SketchManager.CreateCircle(0#, 0#, 0#, radius, 0#, 0#)
  156.  
  157. 'circle extrusion
  158. Dim myFeature As Object
  159. Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, length, length, False, False, False, False, 0.01, 0.01, False, False, False, False, True, True, True, 0, 0, False)
  160.  
  161. 'repetition by sketch
  162. boolstatus = Part.Extension.SelectByID2("Esquisse3D1", "SKETCH", 0, 0, 0, False, 64, Nothing, 0)
  163. boolstatus = Part.Extension.SelectByID2("Boss.-Extru.1", "SOLIDBODY", 0, 0, 0, True, 256, Nothing, 0)
  164. Part.FeatureManager.FeatureSketchDrivenPattern True, False
  165.  
  166. 'delete the first revolution
  167. boolstatus = Part.Extension.SelectByID2("Boss.-Extru.1", "SOLIDBODY", 0, 0, 0, True, 0, Nothing, 0)
  168. Set myFeature = Part.FeatureManager.InsertDeleteBody2(False)
  169.  
  170. 'hide the 3D sketch
  171. boolstatus = Part.Extension.SelectByID2("Esquisse3D1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
  172. Part.BlankSketch
  173. ' Save As REV
  174. longstatus = Part.SaveAs3("C:\Users\Kader\Desktop\test vba\RVE.SLDPRT", 0, 2)
  175.  
  176. Set swmodel = swApp.ActiveDoc
  177. name = "RVE"
  178. bool = swmodel.Extension.SaveAs(name + ".sldprt", 0, 1, Nothing, err, war)
  179.  
  180. End If
  181.  
  182. '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  183.  
  184. ' CUBE
  185.  
  186. ' New Document part
  187. Set Part = swApp.NewDocument("C:\ProgramData\SOLIDWORKS\SOLIDWORKS 2018\templates\Pièce.prtdot", 0, 0, 0)
  188.  
  189. boolstatus = Part.Extension.SelectByID2("Plan de face", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
  190. Dim vSkLines As Variant
  191. vSkLines = Part.SketchManager.CreateCornerRectangle(0, 0, 0, 0.01, 0.01, 0)
  192.  
  193. 'Dim myFeature As Object
  194. Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 6, 0, length, length, False, False, False, False, 0, 0, False, False, False, False, True, True, True, 0, 0, False)
  195. Part.ClearSelection2 True
  196. ' Save As CUBE
  197. longstatus = Part.SaveAs3("C:\Users\Kader\Desktop\test vba\CUBE.SLDPRT", 0, 2)
  198.  
  199. Set swmodel = swApp.ActiveDoc
  200. name = "CUBE"
  201. bool = swmodel.Extension.SaveAs(name + ".sldprt", 0, 1, Nothing, err, war)
  202.  
  203. '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  204.  
  205. ' Ass
  206.  
  207. ' New Document Ass
  208. Set Part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2018\templates\Assemblage.asmdot", 0, 0, 0)
  209.  
  210. ' Insert Component CUBE
  211. Dim swInsertedComponent As Component2
  212. Set swInsertedComponent = Part.AddComponent5("C:\Users\Kader\Desktop\test vba\CUBE.SLDPRT", 0, "", False, "", 0, 0, 0)
  213. swApp.CloseDoc "C:\Users\Kader\Desktop\test vba\CUBE.sldprt"
  214.  
  215. ' Insert Component RVE
  216. Set swInsertedComponent = Part.AddComponent5("C:\Users\Kader\Desktop\test vba\RVE.SLDPRT", 0, "", False, "", 0, 0, 0)
  217. swApp.CloseDoc "C:\Users\Kader\Desktop\test vba\RVE.sldprt"
  218.  
  219. ' Named View
  220. Part.ShowNamedView2 "*Isométrique", 7
  221. Part.ViewZoomtofit2
  222.  
  223. ' Save As CUBE
  224. longstatus = Part.SaveAs3("C:\Users\Kader\Desktop\test vba\Projet7.sldasm", 0, 2)
  225.  
  226. Set swmodel = swApp.ActiveDoc
  227. name = "Projet7"
  228. bool = swmodel.Extension.SaveAs(name + ".sldasm", 0, 1, Nothing, err, war)
  229.  
  230.  
  231. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement