Sie sind auf Seite 1von 11

MSc Advance Industrial & Manufacturing Systems 1/11

3rd Lecture

Connecting API with other Applications/Programs

Some times is useful or even a necessity to load other applications in order


to read, write or simply use then as a function, within the API environment.
There are two ways to do so. First of all we must search to find out whether
the application has API. If so, it is easier to use it, if not then we could
create txt files so that the other application can read the stored data. On
other helpful way is to create .dll (Dynamic Link Libraries) files and use
them as functions is the API.

Loading Object Libraries

If we want to have access to Excel’s object library through the Inventor’s


API, we must first load it by following the below steps.

i. Through the API environment we select the Tools drop-down menu and
we click on References…

Sagias Vasileios Advanced CAD/CAM Systems – ME7722 Dr.-Ing Constantinos Stergiou


MSc Advance Industrial & Manufacturing Systems 2/11

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.

Sagias Vasileios Advanced CAD/CAM Systems – ME7722 Dr.-Ing Constantinos Stergiou


MSc Advance Industrial & Manufacturing Systems 3/11
How to store data to files

Open Statement

A simple way of saving (or reading) data into files is by using this statement.

Syntax

Open <pathname> For mode [Access access] [lock] As [#]filenumber


[Len=reclength]

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.

Access Optional. Keyword specifying the operations permitted on the open


file: Read, Write, or Read Write.

Lock Optional. Keyword specifying the operations restricted on the open


file by other processes: Shared, Lock Read, Lock Write, and Lock
Read Write.

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

Public Sub Save_Values(ArrayIn, name As String)

Dim Tot As Long


Dim i As Long

Tot = UBound(ArrayIn)

Open name For Output As #1

For i = 1 To Tot

Write #1, ArrayIn(i)


Next

Close #1

End Sub

Sagias Vasileios Advanced CAD/CAM Systems – ME7722 Dr.-Ing Constantinos Stergiou


MSc Advance Industrial & Manufacturing Systems 4/11
The Above Example shows a Subroutine that is able to store an array
(ArrayIn) to a text file using a name typed by the user.

Using the above subroutine, write an other one that will use it, in order to
have a running program.

About dynamic Arrays

ReDim Statement

This statement is used to reallocate storage space for dynamic array


variables.

Syntax

ReDim [Preserve] <name>(<lower bound> To <upper bound>)

Preserve (Optional). Keywords used to preservethe data in an existing


array when you change the size of the last dimension

Public Sub ReDim_Array_Example()

‘Define one Array (double precision)


Dim oArr() As Double

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

Redim Preserve oArr(1 to 7)


oArr(6)=13
oArr(7)=11

End Sub

Sagias Vasileios Advanced CAD/CAM Systems – ME7722 Dr.-Ing Constantinos Stergiou


MSc Advance Industrial & Manufacturing Systems 5/11
Case Study 1

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.

Public Sub ExcelOutput()

‘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)

‘Define an integer to use it for the “For…Next” statement


Dim oInt As Integer

‘We define the first item of the array as zero


oX(1) = 0

‘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

‘We define the first item of the array as zero


oY(1) = 0

‘In this loop we fill the oY array.


For oInt = LBound(oY) + 1 To UBound(oY)
oY(oInt) = 2 * oY(oInt - 1) + 1.25
Next

Dim ExcelS As Object

‘We create an Excel Sheet


Set ExcelS = CreateObject("Excel.Sheet")

Dim Message, Title, Default, MyValue


Dim oFile As String

Message = "Enter a name for the Excel file"


Title = "Excel File Name"
Default = "New"

‘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

‘The Excel application will run in the background


ExcelS.Application.Visible = False

‘Error handling

Sagias Vasileios Advanced CAD/CAM Systems – ME7722 Dr.-Ing Constantinos Stergiou


MSc Advance Industrial & Manufacturing Systems 6/11
‘If the Excel application is not installed on the pc, then…
If ExcelS Is Nothing Then

‘…will inform the user to install it and …


MsgBox "In order to be able to see the results you must install the Microsoft Excel Program
in your computer.", vbOKOnly, "I N F O R M A T I O N"

‘…then will overlook the lines of code until it finds the tag “115”
GoTo 115

End If

‘We will use this to change the name of the sheet


‘The name of the Sheet should be a String and written between quotes
Const SheetName = "Data from Inventor"

Dim WorkSheet As Object 'The sheet that all the data will be placed
Dim WorkBook As Object 'The workbook that the active sheet belongs

‘We add a workbook that we will work in


Set WorkBook = ExcelS.Application.Workbooks.Add

Set WorkSheet = WorkBook.Sheets(1) ‘We choose the sheet we would like to work

WorkSheet.Name = SheetName ‘We change the sheet name

Dim SN As Long
Dim i As Long
Dim dblValue As Double
Dim intValue As Long

‘The range of cells will have bold fonts


WorkSheet.Rows("18:18").Font.Bold = True

‘The range of cells will have italic fonts


WorkSheet.Rows("18:18").Font.Italic = True

‘We choose the color of the fonts


WorkSheet.Rows("18:18").Font.ColorIndex = 3

‘We choose the cells’s interior color


WorkSheet.Rows("18:18").Interior.ColorIndex = 35

‘We choose to have autofit for the colums


WorkSheet.Columns("B:N").EntireColumn.AutoFit

‘We choose the alignment in the cells to be center


WorkSheet.Columns("B:N").EntireColumn.HorizontalAlignment = xlCenter

‘How to find the desire cell: Cells(Row,Colum)



WorkSheet.Cells(1, 1).Font.Bold = True ‘Using bold fonts
WorkSheet.Cells(2, 1).Font.Bold = True

Sagias Vasileios Advanced CAD/CAM Systems – ME7722 Dr.-Ing Constantinos Stergiou


MSc Advance Industrial & Manufacturing Systems 7/11
WorkSheet.Cells(1, 1).Font.Size = 12 ‘Change the font size
WorkSheet.Cells(2, 1).Font.Size = 11

‘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(4, 1).Value = "Prof.Dr.-Ing Constantinos Stergiou"


WorkSheet.Cells(5, 1).Value = "Sagias Vasileios(Vasilis) MSc, PhD cand."

WorkSheet.Cells(7, 1).Font.Bold = True


WorkSheet.Cells(8, 1).Font.Bold = True

WorkSheet.Cells(7, 1).Font.Size = 10
WorkSheet.Cells(8, 1).Font.Size = 10

WorkSheet.Cells(7, 1).Value = "Technological Educational Institute of Piraeus"


WorkSheet.Cells(8, 1).Value = "Kingston University London"

WorkSheet.Cells(9, 1).Value = "March 2010 - Athens,Greece"


WorkSheet.Cells(10, 1).Value = "=today()" ‘Displays today’s date

WorkSheet.Cells(18, 1).Value = "S/N"

WorkSheet.Cells(18, 2).Value = "X"


WorkSheet.Cells(18, 3).Value = "Y"

Dim Tot_xy As Integer

Tot_xy = UBound(oX) ‘Finds the upper bound of the oX array

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

Dim A_Chart As Object 'The chart of Y=f(X)

Set A_Chart = WorkBook.Charts.Add ‘Add a charts

A_Chart.Name = "Chart X=f(Y)" ‘Define the name of the chart


Sagias Vasileios Advanced CAD/CAM Systems – ME7722 Dr.-Ing Constantinos Stergiou
MSc Advance Industrial & Manufacturing Systems 8/11

Dim strSvRange As String

‘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

‘By using the With…End With statement we control the chart


With A_Chart

.ChartType = xlXYScatterLinesNoMarkers ‘Define the chart type


.ChartArea.Font.Name = "Arial" ‘Define the used font type
.ChartArea.Font.FontStyle = "Regular" ‘Define the font style
.ChartArea.Font.Size = 8 ‘Define the font size
.PlotArea.Interior.ColorIndex = xlNone ‘Define the color of the plot area

.HasTitle = True ‘Define that we want to have a title


.ChartTitle.Font.Name = "Arial" ‘Define the font type of the title
.ChartTitle.Font.FontStyle = "Regular" ‘Define the font style
.ChartTitle.Font.Size = 11 ‘Define the font size
.ChartTitle.Font.Bold = True ‘Setting the font to bold
.ChartTitle.Font.Color = RGB(0, 0, 255) ‘Define the font color using RGB
.ChartTitle.Text = "AIMS Chart"

.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

Sagias Vasileios Advanced CAD/CAM Systems – ME7722 Dr.-Ing Constantinos Stergiou


MSc Advance Industrial & Manufacturing Systems 9/11

.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"

‘Save the file using the desire file name


WorkSheet.SaveAs strFileName

‘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"

‘Quit the application


WorkSheet.Application.Quit

‘Unloads the worksheet


Set WorkSheet = Nothing

115

End Sub

Sagias Vasileios Advanced CAD/CAM Systems – ME7722 Dr.-Ing Constantinos Stergiou


MSc Advance Industrial & Manufacturing Systems 10/11
In class workout

Having the Case Study 1 as help try to write a subroutine that will :

(First sketch a rectangle and extrude it)

1. Read an opened Part file

2. Finds the center of mass, and the volume of the part

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

Sagias Vasileios Advanced CAD/CAM Systems – ME7722 Dr.-Ing Constantinos Stergiou


MSc Advance Industrial & Manufacturing Systems 11/11
Solutions

i. Example Solution

Public Sub Main()

Dim oAr() As Double


Dim oNa As String

ReDim oAr(1 To 3)
oAr(1) = 15.23423423
oAr(2) = 20.38402934234
oAr(3) = 23409.234234

oNa = "This_is_a_test.txt"

Call Save_Values(oAr, oNa)

End Sub

ii. In class workout solution

Public Sub CenterOfMass_Volume()

‘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

‘The center of mass is a point with x,y and z coordinates


oCoM(1) = oMP.CenterOfMass.X
oCoM(2) = oMP.CenterOfMass.Y
oCoM(3) = oMP.CenterOfMass.Z

oVol = oMP.Volume

End Sub

Sagias Vasileios Advanced CAD/CAM Systems – ME7722 Dr.-Ing Constantinos Stergiou

Das könnte Ihnen auch gefallen