Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.22 KB | None | 0 0
  1. Sub Main()
  2. ' First read the active part file from NX.
  3. ' Then, loop through the sheets.
  4. ' Then, loop through the dimensions in the sheet
  5. ' Grab the zone, text, tolerance type, tolerance and the measurement direction for each.
  6. ' Grab the nearest annotation in that zone.
  7. ' Build an array or a dictionary (similar to JSON or Python's Dictionary class)
  8. ' Then, loop through the text in the document, and do the same steps, except for those that are inapplicable, of course.
  9. ' Ask for a savefile location.
  10. ' Create an Excel file in that location
  11. ' Open the Excel file.
  12. ' Write array to the excel file.
  13.  
  14. If IsNothing(theSession.Parts.Work) Then 'Error handling
  15. MsgBox("This code requires an active part. Please open a drawing first and then try again.")
  16. Exit Sub
  17. End If
  18. Dim workPart as Part
  19. workPart = theSession.Parts.Work
  20.  
  21. Dim theUISession as UI = UI.getUI
  22. Dim saveFileName as String
  23. Dim saveDialog as New System.Windows.Forms.SaveFileDialog
  24. Dim objExcel as Object
  25. Dim objWorkbook as Object
  26. Dim excelFileExists as Boolean = False
  27. Dim objWorksheet as Object
  28. Dim colSheetNumber as Integer = 1
  29. Dim colZone as Integer = 2
  30. Dim colBalloon as Integer = 3
  31. Dim colDescrMatlDir as Integer = 4
  32. Dim colNomValue as Integer = 5
  33. Dim colTolerType as Integer = 6
  34. Dim colTolerUpper as Integer = 7
  35. Dim colTolerLower as Integer = 8
  36. Dim colMaxValue as Integer = 9
  37. Dim colMinValue as Integer = 10
  38. Dim colComment as Integer = 11
  39.  
  40. Dim valueSheetNumber as String
  41. Dim valueZone as String
  42. Dim valueBalloon as String
  43. Dim valueDescrMatlDir as String
  44. Dim valueNomValue as String
  45. Dim valueTolerType as String
  46. Dim valueTolerUpper as String
  47. Dim valueTolerLower as String
  48. Dim valueMaxValue as String
  49. Dim valueMinValue as String
  50. Dim valueComment as String
  51.  
  52. Dim rowNumber as Integer = 1
  53.  
  54. With saveDialog
  55. .DefaultExt = "xlsx"
  56. .FileName = "Exported Data"
  57. .Filter = "MS Excel Spreadsheets (*.xlsx)|*.xlsx|All Files (*.*)|*.*"
  58. .FilterIndex = 1
  59. .OverwritePrompt = True
  60. .Title = "Select a file where you'd like to save the exported data."
  61. End With
  62. saveDialog.ShowDialog()
  63. saveFileName = saveDialog.FileName
  64. If saveFileName ="Exported Data" Then
  65. MsgBox("You failed to select a save file. Exitting the macro.")
  66. Exit Sub
  67. End If
  68. objExcel = CreateObject("Excel.Application")
  69. if objExcel is Nothing Then
  70. theUISession.NXMessageBox.Show("Error", theUISession.NXMessageBox.DialogType.Error, "Could not start Excel, journal exiting")
  71. Exit Sub
  72. End If
  73. If File.Exists(saveFileName) Then
  74. excelFileExists = True
  75. objWorkbook = objExcel.Workbooks.Open(saveFileName)
  76. objWorksheet = objWorkbook.Sheets.Add
  77. Else
  78. objWorkbook = objExcel.Workbooks.Add
  79. objWorkbook.saveAs(saveFileName)
  80. objWorksheet = objWorkbook.Sheets(1)
  81. End If
  82.  
  83. objWorksheet.cells(rowNumber, colSheetNumber).Value = "Sheet Number"
  84. objWorksheet.cells(rowNumber, colZone).Value = "Drawing Zone"
  85. objWorksheet.cells(rowNumber, colBalloon).Value = "Balloon Number"
  86. objWorksheet.cells(rowNumber, colDescrMatlDir).Value = "Description Text Measurement Direction"
  87. objWorksheet.cells(rowNumber, colNomValue).Value = "Nominal Value"
  88. objWorksheet.cells(rowNumber, colTolerType).Value = "Tolerance Type"
  89. objWorksheet.cells(rowNumber, colTolerUpper).Value = "Upper Tolerance"
  90. objWorksheet.cells(rowNumber, colTolerLower).Value = "Lower Tolerance"
  91. objWorksheet.cells(rowNumber, colMaxValue).Value = "Maximum Value"
  92. objWorksheet.cells(rowNumber, colMinValue).Value = "Minimum Value"
  93.  
  94. For Each tempDim as Annotations.Dimension in workPart.Dimensions
  95. rowNumber = rowNumber + 1
  96.  
  97. Dim theSheet as Drawings.DrawingSheet = AskDrawingSheet(tempDim)
  98. valueSheetNumber = getSheetNumber(theSheet).ToString
  99. valueZone = getZone(theSheet, tempDim)
  100. valueBalloon = ""
  101. valueNomValue = tempDim.ComputedSize
  102.  
  103. valueDescrMatlDir = tempDim.GetType.ToString
  104. ' Clean up the material direction.
  105. valueDescrMatlDir = Replace(valueDescrMatlDir, "NXOpen.Annotations.","")
  106.  
  107. Select Case valueDescrMatlDir
  108. Case "DiameterDimension"
  109. valueDescrMatlDir = "ø"
  110. Case "CylindricalDimension"
  111. valueDescrMatlDir = "ø"
  112. Case "MinorAngularDimension"
  113. valueDescrMatlDir = "∠"
  114. Case "HorizontalDimension"
  115. valueDescrMatlDir = "Length"
  116. End Select
  117.  
  118. valueTolerLower = "-"
  119. valueTolerUpper = "-"
  120. valueMaxValue = "-"
  121. valueMinValue = "-"
  122. valueComment = "-"
  123.  
  124. valueTolerType = tempDim.ToleranceType.ToString
  125. Select Case valueTolerType
  126. Case Is = "UnilateralBelow"
  127. valueTolerLower = tempDim.LowerMetricToleranceValue.ToString
  128. valueMaxValue = tempDim.ComputedSize
  129. valueMinValue = tempDim.ComputedSize + tempDim.LowerMetricToleranceValue
  130. Case Is = "UnilateralAbove"
  131. valueTolerUpper = tempDim.UpperMetricToleranceValue.ToString
  132. valueMaxValue = tempDim.ComputedSize + tempDim.UpperMetricToleranceValue
  133. valueMinValue = tempDim.ComputedSize
  134. Case Is = "LimitsAndFits"
  135. valueTolerLower = tempDim.LimitFitDeviation
  136. valueTolerUpper = tempDim.LimitFitGrade
  137. valueMaxValue = "As per standards"
  138. valueMinValue = "As per standards"
  139. Case Is = "BilateralOneLine"
  140. valueTolerUpper = tempDim.UpperMetricToleranceValue.ToString
  141. valueTolerLower = (-1*tempDim.UpperMetricToleranceValue).ToString
  142. valueMaxValue = tempDim.ComputedSize + tempDim.UpperMetricToleranceValue
  143. valueMinValue = tempDim.ComputedSize - tempDim.UpperMetricToleranceValue
  144. Case Is = "BilateralTwoLines"
  145. valueTolerLower = tempDim.LowerMetricToleranceValue.ToString
  146. valueTolerUpper = tempDim.UpperMetricToleranceValue.ToString
  147. valueMaxValue = tempDim.ComputedSize + tempDim.UpperMetricToleranceValue
  148. valueMinValue = tempDim.ComputedSize + tempDim.LowerMetricToleranceValue
  149. Case Is = "None"
  150. valueTolerUpper = "NA"
  151. valueTolerLower = "NA"
  152. valueMaxValue = tempDim.ComputedSize
  153. valueMinValue = tempDim.ComputedSize
  154. Case Is = "Basic"
  155. valueTolerUpper = "NA"
  156. valueTolerLower = "NA"
  157. valueMaxValue = tempDim.ComputedSize
  158. valueMinValue = tempDim.ComputedSize
  159. Case Is = "LimitOneLine"
  160. valueTolerUpper = tempDim.UpperMetricToleranceValue.ToString
  161. valueTolerLower = tempDim.LowerMetricToleranceValue.ToString
  162. valueMaxValue = tempDim.ComputedSize + tempDim.UpperMetricToleranceValue
  163. valueMinValue = tempDim.ComputedSize + tempDim.LowerMetricToleranceValue
  164. Case Else
  165. valueComment = "Error, " & valueTolerType & " is a tolerance type that hasn't been accounted for in the code. Please modify the script."
  166. End Select
  167.  
  168.  
  169. objWorksheet.cells(rowNumber, colSheetNumber).Value = valueSheetNumber
  170. objWorksheet.cells(rowNumber, colZone).Value = valueZone
  171. objWorksheet.cells(rowNumber, colBalloon).Value = valueBalloon
  172. objWorksheet.cells(rowNumber, colDescrMatlDir).Value = valueDescrMatlDir
  173. objWorksheet.cells(rowNumber, colNomValue).Value = valueNomValue
  174. objWorksheet.cells(rowNumber, colTolerType).Value = valueTolerType
  175. objWorksheet.cells(rowNumber, colTolerUpper).Value = valueTolerUpper
  176. objWorksheet.cells(rowNumber, colTolerLower).Value = valueTolerLower
  177. objWorksheet.cells(rowNumber, colMaxValue).Value = valueMaxValue
  178. objWorksheet.cells(rowNumber, colMinValue).Value = valueMinValue
  179. objWorksheet.cells(rowNumber, colComment).Value = valueComment
  180. Next
  181.  
  182. for each annote as Annotation in workPart.Annotations
  183.  
  184. Next
  185.  
  186. objWorkbook.save()
  187. objWorkbook.close()
  188. objExcel.Quit()
  189. objWorksheet = Nothing
  190. objWorkbook = Nothing
  191. objExcel = Nothing
  192. MsgBox("Completed the extraction successfully! Check " & saveFileName & " for the data.")
  193.  
  194. End Sub
  195.  
  196. Function getSheetNumber(ByVal theSheet as Drawings.DrawingSheet) As String
  197. ' This function returns the number of a sheet given the DrawingSheet object.
  198. Dim sheetNum as Integer
  199. Dim theSheetBuilder as Drawings.DrawingSheetBuilder
  200. theSheetBuilder = theSession.Parts.Work.DrawingSheets.DrawingSheetBuilder(theSheet)
  201. sheetNum = theSheetBuilder.Number
  202. theSheetBuilder.Destroy()
  203. Return sheetNum.ToString
  204. End Function
  205.  
  206.  
  207. Function AskDrawingSheet(ByVal theObject As TaggedObject) As Drawings.DrawingSheet
  208.  
  209. Dim theView As View = TryCast(theObject, View)
  210. If Not theView Is Nothing Then
  211. Dim sheetTag As Tag = Nothing
  212. Try
  213. theUfSession.Draw.AskDrawingOfView(theView.Tag, sheetTag)
  214. Return Utilities.NXObjectManager.Get(sheetTag) ' the drawing it is on
  215. Catch ex As NXException
  216. Return Nothing ' it is a model view
  217. End Try
  218. End If
  219.  
  220. Dim viewName As String = Nothing
  221. Dim status As Integer = Nothing
  222. Try
  223. theUfSession.View.AskViewDependentStatus(theObject.Tag, status, viewName)
  224. Catch ex As NXException
  225. Return Nothing
  226. End Try
  227. If status = 0 Then Return Nothing ' it is a model mode object
  228.  
  229. Dim viewTag As Tag = Nothing
  230. theUfSession.View.AskTagOfViewName(viewName, viewTag)
  231. Dim viewType As Integer = Nothing
  232. Dim viewSubtype As Integer = Nothing
  233. theUfSession.View.AskType(viewTag, viewType, viewSubtype)
  234. If viewType = 0 Then Return Nothing ' it is view dependent in a modeling view
  235.  
  236. Dim drawingTag As Tag = Nothing
  237. theUfSession.Draw.AskDrawingOfView(viewTag, drawingTag)
  238. Return Utilities.NXObjectManager.Get(drawingTag) ' the drawing it is on!
  239.  
  240. End Function
  241.  
  242. Function getZone(ByVal theSheet As Drawings.DrawingSheet, ByVal theDimension As Annotations.Dimension) As String
  243. Dim borderBuilder As Drawings.BordersAndZonesBuilder
  244. borderBuilder = theSession.Parts.Work.Drafting.BordersAndZonesObjects.CreateBordersAndZonesBuilder(theSheet.BordersAndZones)
  245.  
  246. Dim numHorizontalZones As Integer = (theSheet.Length - borderBuilder.LeftMargin - borderBuilder.RightMargin) / borderBuilder.HorizontalSize
  247. Dim numVerticalZones As Integer = (theSheet.Height - borderBuilder.BottomMargin - borderBuilder.TopMargin) / borderBuilder.VerticalSize
  248.  
  249. 'calculate zone wrt bottom left of drawing (ZoneOrigin.BottomLeft)
  250. Dim Hcell As Double = (theDimension.AnnotationOrigin.X - borderBuilder.LeftMargin) / borderBuilder.HorizontalSize
  251. Dim Vcell As Double = (theDimension.AnnotationOrigin.Y - borderBuilder.BottomMargin) / borderBuilder.VerticalSize
  252.  
  253. Hcell = Math.Ceiling(Hcell)
  254. Vcell = Math.Ceiling(Vcell)
  255.  
  256. Dim theZoneOrigin As Drawings.BordersAndZonesBuilder.ZoneOrigin = borderBuilder.Origin
  257. borderBuilder.Destroy()
  258.  
  259. Dim verticalLetterNum As Integer
  260. Dim verticalLetter As Char
  261.  
  262. Dim horizontalNum As Integer
  263.  
  264. If theZoneOrigin = Drawings.BordersAndZonesBuilder.ZoneOrigin.BottomLeft Or theZoneOrigin = Drawings.BordersAndZonesBuilder.ZoneOrigin.TopLeft Then
  265. horizontalNum = Hcell
  266. Else
  267. 'origin on right side
  268. horizontalNum = numHorizontalZones - Hcell + 1
  269. End If
  270.  
  271. If theZoneOrigin = Drawings.BordersAndZonesBuilder.ZoneOrigin.BottomLeft Or theZoneOrigin = Drawings.BordersAndZonesBuilder.ZoneOrigin.BottomRight Then
  272. verticalLetterNum = Asc("A") + Vcell - 1
  273. verticalLetter = Chr(verticalLetterNum)
  274.  
  275. Else
  276. 'origin on the top
  277. verticalLetterNum = Asc("A") + numVerticalZones - Vcell
  278. verticalLetter = Chr(verticalLetterNum)
  279.  
  280. End If
  281. Return verticalLetter & horizontalNum.ToString
  282. End Function
  283.  
  284. Public Function GetUnloadOption(ByVal dummy As String) As Integer
  285. 'Unloads the image immediately after execution within NX
  286. GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
  287. '-------------------------------
  288. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement