Forums:
Hi,
I have bodies grouped together in a part. I want to generate a excel report with each group mass properties. While compile I got error saying feature group cannot be cast into Brep, If I use Brep it is selecting all the individual bodies inside group not considering the group.
Option Strict Off Imports System Imports System.IOImports System.Windows.FormsImports System.DrawingImports System.Drawing.ImagingImports System.CollectionsImports System.Runtime.InteropServicesImports NXOpen Imports NXOpen.GeometricUtilitiesImports NXOpen.UFImports NXOpen.AssembliesImports NXOpenUI Imports System.Collections.GenericImports System.ThreadingImports Microsoft.OfficeImports Microsoft.Office.Interop.ExcelImports Microsoft.Office.Interop Module MassandCG Dim theSession As Session = Session.GetSession()Dim workPart As Part = theSession.Parts.WorkDim displayPart As Part = theSession.Parts.DisplayDim theUI As UI = UI.GetUIDim ufs As UFSession = UFSession.GetUFSessionDim lw As ListingWindow = theSession.ListingWindow Sub Main() Dim MyExcel AsObject=NothingDim MyBook AsObject=NothingDim Mysheet AsObject=NothingDim RefBook AsObject=NothingDim RefSheet AsObject=NothingDim ExcelFileName AsString="C:\Users\t5371pa\Desktop\Checklist.xlsx"Dim ExcelNewFileName AsString="C:\Users\t5371pa\Desktop\Myexcel.xlsx"Dim ExcelFileExists AsBoolean=FalseDim NewExcel AsString=Nothing'Dim MyExcel As Excel.Application = Nothing'Dim MyBook As Workbook = Nothing'Dim Mysheet As Worksheet = Nothing'Dim RefBook As Workbook = Nothing'Dim RefSheet As Worksheet = Nothing Dim Partno AsString=NothingDim tempScreenshot AsString=NothingDim tempLocation AsString="C:\Temp"Try Partno = workPart.GetStringAttribute("DB_PART_NO")Catch ex As NXException lw.WriteLine("Partno Not found"& ex.ErrorCode&":"& ex.Message.ToString) Partno ="Not Found"EndTry 'Create Excel File and Save it in user defined locationDim Savechecklist AsNew SaveFileDialog With Savechecklist .Title="Saving Standard DP Check File".InitialDirectory="C:\Users\t5371pa\Desktop".Filter="ExcelFiles (*.xlsx)|*.xlsx|Macro enabled Powerpoint files (*.xlsm)|*.xlsm|All files (*.*)|*.*".FilterIndex=1.RestoreDirectory=True.FileName= ExcelNewFileName If.ShowDialog= DialogResult.OKThen NewExcel =.FileNameEndIfEndWith If MyExcel IsNothingThen MyExcel =CreateObject("Excel.Application")If MyExcel IsNothingThen theUI.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "Could not start Excel, journal exiting")ExitSubEndIfEndIf MyExcel.Visible=False If File.Exists(ExcelNewFileName)Then'Open the Excel file ExcelFileExists =True MyBook = MyExcel.Workbooks.Open(ExcelNewFileName)Else'Create the Excel file MyBook = MyExcel.Workbooks.Add MyBook.SaveAs(ExcelNewFileName)EndIfIf MyBook IsNothingThen theUI.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "Could not open Excel file: "& ExcelFileName & ControlChars.NewLine&"journal exiting.")ExitSubEndIf Mysheet = MyBook.Worksheets.Add() Mysheet.Activate() Dim Row AsInteger=NothingDim i AsInteger=NothingDim MyGroupRep As NXObject =NothingDim MyGroup As Features.Brep=NothingDim Replist As ArrayList =New ArrayList Dim Mylist As ArrayList =New ArrayList ForEach MyGroupRep In workPart.FeaturesIfTypeOf MyGroupRep Is Features.BrepThen Replist.Add(MyGroupRep)EndIfNext ForEach MyGroup In Replist Mylist.Add(MyGroup)NextMsgBox(Mylist.Count.ToString) For Row =1To Mylist.Count-1Dim Thegroup As Features.Brep= Mylist(Row)Dim GruopName AsString= Thegroup.Name Thegroup.ShowBody(False)Dim BodyList()As Body =NothingDim MYSolidBodies As List(Of Body)=NothingDim MYSheetBodies As List(Of Body)=Nothing BodyList = Thegroup.GetBodies() For j =1To BodyList.Length-1If BodyList(j).IsSolidBodyThen MYSolidBodies.Add(BodyList(i))ElseIf BodyList(j).IsSheetBodyThen MYSheetBodies.Add(BodyList(i))EndIfNextMsgBox(MYSolidBodies.Count.ToString)Dim thebodies()As Body = MYSolidBodies.ToArrayDim Partvolume AsString=NothingDim PartDen AsString=NothingDim PartMass AsString=NothingDim CGValue As Point3d =NothingDim CGX AsString=NothingDim CGY AsString=NothingDim CGZ AsString=Nothing Dim myMeasure As MeasureManager = theSession.Parts.Display.MeasureManager()Dim massUnits(4)As Unit massUnits(0)= theSession.Parts.Display.UnitCollection.GetBase("Area") massUnits(1)= theSession.Parts.Display.UnitCollection.GetBase("Volume") massUnits(2)= theSession.Parts.Display.UnitCollection.GetBase("Mass") massUnits(3)= theSession.Parts.Display.UnitCollection.GetBase("Length") Dim MyNewmeasure As MeasureBodies =NothingTry MyNewmeasure = myMeasure.NewMassProperties(massUnits, 0.99, thebodies) MyNewmeasure.InformationUnit= MeasureBodies.AnalysisUnit.KilogramMillimeterTry Partvolume = MyNewmeasure.Volume.ToString("#.0")Catch ex As NXException Partvolume = MyNewmeasure.Volume.ToString("#.0")EndTryTry PartMass = MyNewmeasure.Mass.ToString("#.0")Catch ex As NXException PartMass = MyNewmeasure.Mass.ToString("#.0")EndTryTry CGValue = MyNewmeasure.Centroid CGX = CGValue.X.ToString("#.0") CGY = CGValue.Y.ToString("#.0") CGZ = CGValue.Z.ToString("#.0")Catch ex As NXException CGX =0 CGY =0 CGZ =0EndTryTryDim DenFor AsInteger=(MyNewmeasure.Mass.ToString/ MyNewmeasure.Volume.ToString) PartDen = DenFor.ToString("#.0")Catch ex As NXException PartDen =(MyNewmeasure.Mass.ToString/ MyNewmeasure.Volume.ToString).ToString("#.0")EndTryCatch ex As Exception lw.WriteLine(Partno &": MassProperties error-"& ex.Message.ToString)EndTry Try tempScreenshot = IO.Path.Combine(tempLocation, Partno &"-"& Row.ToString&".jpg") ExportScreenshot(tempScreenshot) Catch ex As NXException lw.WriteLine(Partno & Row.ToString&": View Not Generated-"& ex.ErrorCode&":"& ex.Message.ToString)EndTry Mysheet.cells(Row, 1).value()= GruopName Dim Piccommant AsObject= Mysheet.Cells(Row, 2).AddComment() Mysheet.cells(Row, 3).value()= Partvolume Mysheet.cells(Row, 4).value()= PartMass Mysheet.cells(Row, 5).value()= PartDen Mysheet.cells(Row, 6).value()= CGX Mysheet.cells(Row, 7).value()= CGY Mysheet.cells(Row, 8).value()= CGZ 'Dim Piccommant As Excel.Comment = Mysheet.Cells(Row, 3).AddComment()With Piccommant.Shape.Fill.UserPicture(tempLocation &"\"& Partno &"-"& Row.ToString&".jpg").Height=600.Width=600EndWith Thegroup.HideBody()Next MyBook.Save() MyBook.Close() MyBook =Nothing MyExcel.Quit() EndSub Sub ExportScreenshot(ByVal filename AsString) 'save user preference for visibility of WCS, triad, view name, and view borderDim wcsVisible AsBoolean= theSession.Parts.BaseDisplay.WCS.VisibilityDim triadVisible AsInteger= theSession.Preferences.ScreenVisualization.TriadVisibilityDim displayModelViewNames AsBoolean= theSession.Parts.BaseDisplay.Preferences.NamesBorderVisualization.ShowModelViewNamesDim displayModelViewBorders AsBoolean= theSession.Parts.BaseDisplay.Preferences.NamesBorderVisualization.ShowModelViewBorders 'turn off the WCS, triad, view name, and view border theSession.Parts.BaseDisplay.WCS.Visibility=False theSession.Preferences.ScreenVisualization.TriadVisibility=False theSession.Parts.BaseDisplay.Preferences.NamesBorderVisualization.ShowModelViewBorders=False theSession.Parts.BaseDisplay.Preferences.NamesBorderVisualization.ShowModelViewNames=False Try ufs.Disp.CreateImage(filename, UFDisp.ImageFormat.Jpeg, UFDisp.BackgroundColor.White)Catch ex As Exception MsgBox(ex.Message& ControlChars.NewLine& _ "'"& filename &"' could not be created")ThrowNew Exception("Screenshot could not be created")Finally'reset visibility of WCS, triad, view name, and view border to user's preference theSession.Parts.BaseDisplay.WCS.Visibility= wcsVisible theSession.Preferences.ScreenVisualization.TriadVisibility= triadVisible theSession.Parts.BaseDisplay.Preferences.NamesBorderVisualization.ShowModelViewBorders= displayModelViewBorders theSession.Parts.BaseDisplay.Preferences.NamesBorderVisualization.ShowModelViewNames= displayModelViewNames EndTry EndSubEndModule