Topsolid online Help Index
This example shows how to start with the API Com and Visual Basic.
Declaration of global variables
Option Explicit
Dim TopApp As TopSolid.Application
Dim TopDoc As TopSolid.DocumentDesign
On the form loading, initialize the application and create a new document .top
Private Sub Form_Load()
' connect Visual Basic with TopSolid
' if TopSolid isn't open, it will open a new session of TopSolid
Set TopApp = New TopSolid.Application
' create a new document *.top
Set TopDoc = TopApp.Documents.Add("top")
End Sub
Creation of a basic circle
Dim TopCircle As TopSolid.Curve
' Create a basic circle
' Centre = 0, 0, 0
' X axis = 1, 0, 0
' Y axis = 0, 1, 0
' Radius = 0.01
Set TopCircle = TopDoc.Curves.AddBasicCircle(0, 0, 0, 1, 0, 0, 0, 1, 0, 0.01)
' Set the name of the circle
TopCircle.Element.Name = "Circle_1"
' Free the memory
Set TopCircle = Nothing
Creation of an extruded shape based on the circle "Circle_1"
Dim TopShape As TopSolid.Shape
Dim TopElt As TopSolid.Element
Dim TopCircle As TopSolid.Curve
On Error Resume Next
' Search the curve to do the extruded
Set TopElt = TopDoc.Document.SearchElementByName("Circle_1")
' "cast" the element in circle
Set TopCircle = TopElt
If TopCircle Is Nothing Then
'error management
Exit Sub
End If
' Create a basic extruded
' Curve = Circle 1
' Z axis = 0, 0, 1
' Length = 0.02
Set TopShape = TopDoc.Shapes.AddBasicExtruded(TopCircle, 0, 0, 1, 0.02)
' Change the color of the shape
TopShape.Element.Color = topColorBlue
' Free the memory
Set TopCircle = Nothing
Set TopShape = Nothing
Set TopElt = Nothing
Save of the document
' Save the document with the name how_to_start.top
' If the document is modified, TopSolid must ask the user
TopDoc.Document.SaveAs "d:\projets\how_to_start2.top", True
Close the document
' TopSolid must save the modification(first 'True')
' TopSolid must ask the user (second 'True')
TopDoc.Document.Close True, True
Close TopSolid
' Close TopSolid
TopApp.Quit
On the form unloading, free memory
Private Sub Form_Unload(Cancel As Integer)
' Free memory
Set TopDoc = Nothing
Set TopApp = Nothing
End Sub
Topsolid online Help Index