Beruflich Dokumente
Kultur Dokumente
3rd Lecture
i. Through the API environment we select the Tools drop-down menu and
we click on References…
ii. On the pop-up menu we search for Microsoft Excel xx Object Library and
check it. Finally we click OK.
After these two steps, we have full access to Excel’s Library. This means
that we are able to use every Excel’s API object through Inventor API.
Open Statement
A simple way of saving (or reading) data into files is by using this statement.
Syntax
Pathname Required. String expression that specifies a file name — may include
directory or folder, and drive.
Mode Required. Keyword specifying the file mode: Append, Binary, Input,
Output, or Random. If unspecified, the file is opened for Random
access.
Filenumber Required. A valid file number in the range 1 to 511, inclusive. Use
the FreeFile function to obtain the next available file number.
Reclength Optional. Number less than or equal to 32,767 (bytes). For files
opened for random access, this value is the record length. For
sequential files, this value is the number of characters buffered.
Example
Tot = UBound(ArrayIn)
For i = 1 To Tot
Close #1
End Sub
Using the above subroutine, write an other one that will use it, in order to
have a running program.
ReDim Statement
Syntax
Redim oArr(1 to 5)
oArr(1)=5
oArr(2)=2
oArr(3)=6
oArr(4)=8
oArr(5)=14
‘Re-dimensioning the Array help us to dynamical control the dimensions of the arrays
‘If we do not use the optional keyword [Preserve] all data in the array will be lost
End Sub
This case study shows a simple usage of Excel’s objects thought Inventors
API. The below subroutines after filling two simple Arrays stores them to an
Excel file and at the same time creates and modifies a Chart.
‘Define two dynamic Arrays (double precision) that will hold x and y coordinates
Dim oX() As Double
Dim oY() As Double
‘Re-dimensioning the Arrays help us to dynamical control the dimensions of the arrays
ReDim oX(1 To 10)
ReDim oY(1 To 10)
‘In this loop we fill the oX array.The results would be an array that: oX(i)-oX(i-1)=5
For oInt = LBound(oX) + 1 To UBound(oX)
oX(oInt) = oX(oInt - 1) + 5
Next
‘Through this pop-up input box the user will be able to type the desire name of the xls file
‘that will be created later
MyValue = InputBox(Message, Title, Default)
oFile = MyValue
‘Error handling
‘…then will overlook the lines of code until it finds the tag “115”
GoTo 115
End If
Dim WorkSheet As Object 'The sheet that all the data will be placed
Dim WorkBook As Object 'The workbook that the active sheet belongs
Set WorkSheet = WorkBook.Sheets(1) ‘We choose the sheet we would like to work
Dim SN As Long
Dim i As Long
Dim dblValue As Double
Dim intValue As Long
‘When we want to add text to a cell then the input should be String
WorkSheet.Cells(1, 1).Value = "MSc Advanced Industrial and Manufacturing Systems"
WorkSheet.Cells(2, 1).Value = "Module: Advance CAD/CAM Systems"
WorkSheet.Cells(4, 1).Font.Size = 10
WorkSheet.Cells(5, 1).Font.Size = 10
WorkSheet.Cells(7, 1).Font.Size = 10
WorkSheet.Cells(8, 1).Font.Size = 10
For i = 1 To Tot_xy
intValue = i
WorkSheet.Cells(i + 18, 1).Value = intValue
dblValue = oX(i)
WorkSheet.Cells(i + 18, 2).Value = dblValue
dblValue = oY(i)
WorkSheet.Cells(i + 18, 3).Value = dblValue
Next
‘Here we set a dynamic range that we will use later. This ranges starts from b19 cell and ends
‘to b(Tot_xy+18) cell. The value of (Tot_xy+18) is calculates from the upper bound of the oX
‘array and then we add 18. 18 are the lines that we want of leave and the start the x,y
‘coordinates.
strSvRange = "b19:b" & Tot_xy + 18
.Axes(xlValue).TickLabels.Font.Bold = True
.Axes(xlValue).TickLabels.Font.Size = 8
.Axes(xlValue).HasTitle = True
.Axes(xlValue).AxisTitle.Text = "Y"
.Axes(xlValue).AxisTitle.Font.Size = 10
.Axes(xlValue).AxisTitle.Font.Bold = True
.Axes(xlValue).AxisTitle.HorizontalAlignment = xlHAlignRight
.Axes(xlValue).HasMajorGridlines = True
.Axes(xlValue).MajorGridlines.Border.Color = RGB(0, 0, 255)
.Axes(xlValue).MajorGridlines.Border.LineStyle = xlDot
.Axes(xlValue).MajorGridlines.Border.Weight = xlThin
.Axes(xlCategory).TickLabels.Font.Bold = True
.Axes(xlCategory).TickLabels.Font.Size = 8
.Axes(xlCategory).HasTitle = True
.Axes(xlCategory).AxisTitle.Text = "X"
.Axes(xlCategory).AxisTitle.Font.Size = 10
.Axes(xlCategory).AxisTitle.Font.Bold = True
.Axes(xlCategory).AxisTitle.VerticalAlignment = xlVAlignTop
.Axes(xlCategory).HasMajorGridlines = True
.Axes(xlCategory).MajorGridlines.Border.Color = RGB(0, 0, 255)
.Axes(xlCategory).MajorGridlines.Border.LineStyle = xlDot
.Axes(xlCategory).MajorGridlines.Border.Weight = xlThin
.HasLegend = False
.SetSourceData Source:=WorkSheet.Range(strSvRange), PlotBy:=xlColumns
End With
‘Here we set a string that will use for the file name of the excel file that is going to be
‘saved later.
Dim strFileName As String
strFileName = "C:\Data_From_Inventor_" & oFile & ".XLS"
‘Informs the user about the name and the folder that the file was saved
MsgBox "The results were saved at : " & strFileName, vbOKOnly, "I N F O R M A T I O N"
115
End Sub
Having the Case Study 1 as help try to write a subroutine that will :
3. Then save the center of mass and its volume to a text file and then in
an Excel file (user input: the desire file name).
Notes
i. Example Solution
ReDim oAr(1 To 3)
oAr(1) = 15.23423423
oAr(2) = 20.38402934234
oAr(3) = 23409.234234
oNa = "This_is_a_test.txt"
End Sub
‘Create a variable that will “hold” the transient geormetry for later use
Dim oTG As TransientGeometry
Set oTG = ThisApplication.TransientGeometry
‘Create a variable that will “hold” the component definition for later use
Dim oCD As ComponentDefinition
Set oCD = ThisApplication.ActiveDocument.ComponentDefinition
‘From the component definition collection assign a variable to the mass properties
Dim oMP As MassProperties
Set oMP = oCD.MassProperties
‘Declare 2 variables to hold the Center Of Mass (oCoM) and the volume (oVol)
Dim oCoM(1 To 3) As Double
Dim oVol As Double
oVol = oMP.Volume
End Sub