Anda di halaman 1dari 20

VBA 4

VBA with Sketch

6
Lecture 1

Creating a line
Dim swApp As Object
Dim Part As Object
Sub main()
S t swApp
Set
A
= A
Application.SldWorks
li ti
SldW k
Set Part = swApp.ActiveDoc
Part.CreateLine2 p1x, p1y, 0, p2x, p2y, 0
OR
Dim line1 As Object
Set line1 = Part.CreateLine2 (p1x, p1y, 0, p2x, p2y, 0)
End Sub

Condition: the sketch p


plane is already
y been set
Lecture 6

What we are going to cover


Entity
E tit tto d
draw: arc, circle,
i l
Selection set
Setting the sketch plane

Lecture 6

Arc: CreateArcByCenter
retval = ModelDoc2.CreateArcByCenter ( p1x, p1y, p1z, p2x, p2y, p2z,
p3x, p3y, p3z )
This method creates an arc by center in this model document.
(
(double)
)
(double)
(double)
(double)
(double)
(double)
(double)
(double)
(double)

p
p1x
p1y
p1z
p2x
p2y
p2z
p3x
p3y
p3z

X coordinate for center p


point in meters
Y coordinate for center point in meters
Z coordinate for center point in meters
X coordinate for start point in meters
Y coordinate for start point in meters
Z coordinate for start point in meters
X coordinate for end point in meters
Y coordinate for end point in meters
Z coordinate for end point in meters

Output:(VARIANT_BOOL) retvalTRUE if successful, FALSE otherwise


boolstatus = Part.CreateArcByCenter(0, 0, 0, 5, 0, 0, -5, 0, 0)

Lecture 6

Arc:CreateArc2
retval = ModelDoc2.CreateArc2 ( xc, yc, zc, xp1, yp1, zp1, xp2,
yp2, zp2, direction )
This method creates an arc based on a center point, a start, end
point, and a direction.
(double) xc
X value of the circle center point in meters
(double) yc
Y value of the circle center point in meters
(double) zc
Z value of the circle center point in meters
(double) xp1
X value of the start point of the arc in meters
(double) yp1
Y value of the start point of the arc in meters
(double) zp1
Z value of the start point of the arc in meters
(double) xp2
X value of the end point of the arc in meters
(double) yp2
Y value of the end point of the arc in meters
(double) zp2
Z value of the end point of the arc in meters
(short) direction
+1 : Go from the start point to the end point in a counter-clockwise
direction
-1 : Go from the start point to the end point in a clockwise direction
Lecture 6

Return:(LPDISPATCH) retvalPointer to the dispatch object of the arc that


was created

What is the difference?


Retval: Boolean Operation
return TRUE is the object successfully created and
FALSE otherwise

Retval:LPDISPATCH
return pointer to the created object

Method to draw the arc using LPDISPATCH


1. Part.CreateArc2 0, 0, 0, 5, 0, 0, -5, 0, 0, 1

2. Dim arc1 As Object


Set arc1 = Part.CreateArc2(0, 0, 0, 5, 0, 0, -5, 0, 0, 1)
Lecture 6

line properties.swp

What is the difference?


Part.CreateArc2
Part
CreateArc2 0,
0 0,
0 0,
0 5,
5 0,
0 0,
0 -5
5, 0
0, 0,
0 1
Just draw the arc
Dim arc1 As Object
Set arc1 = Part.CreateArc2(0, 0, 0, 5, 0, 0, -5, 0, 0, 1)
Draw the arc and name the arc as arc1. Therefore, we
manipulate the arc such as changing the arc properties
etc
Properties can be changed
arc1.Color = 0
arc1.Width = 3
arc1.Style = 2

Lecture 6

Other Create Sketch Entities


Ellipse
retval = ModelDoc2.CreateEllipse2 ( centerX, centerY,
centerZ, majorX, majorY, majorZ, minorX, minorY,
minorZ )
Point
retval = ModelDoc2.CreatePoint2 ( x, y, z )
p
Spline
retval = ModelDoc2.CreateSpline ( pointData )
*point data : array form
Lecture 6

Selection Set
retval = Part.Extension.SelectByID2
Part Extension SelectByID2 ( Name,
Name Type,
Type X,
X Y,
Y Z,
Z
Append, Mark, Callout, SelectOption )
This method selects the specified entity.
(BSTR) Name
Name of object to select or an empty string
(BSTR) Type
Type of object (uppercase) or an empty string
swSelNOTHING
swSelEDGES
S lEDGES
"EDGE
swSelFACES
"FACE
swSelVERTICES
"VERTEX
swSelDATUMPLANES
"PLANE
(double) XX
selection location or 0
(double) YY
selection location or 0
(double) ZZ
selection location or 0

Lecture 6

Selection Set
retval = Part.Extension.SelectByID2 ( Name, Type, X, Y, Z,
Append, Mark, Callout, SelectOption )
Use these value for other parameters
Append : FALSE
Mark

:0

Callout: Nothing
SelectOption: 0

Lecture 6

10

Selection set: Name and Type


LINE
Name:
General line: SketchLine
Specific name: line1
Set line1 = Part.CreateLine2(0, 0, 0, 0, 10, 0)

Type:
"SKETCHSEGMENT"
Lecture 6

11

Selection set: coordinate


Entity creation: local CS depends on
plane
Set line1 = Part
Part.CreateLine2(0,
CreateLine2(0, 0, 0, 0, 10, 0)
Top plane

CS in selection set: global CS


boolstatus = Part.Extension.SelectByID2(
Sk t hLi
SketchLine,
"SKETCHSEGMENT"
"SKETCHSEGMENT", 0,
0 0,
0 -5,
5 False,
F l
0,
0
Nothing, 0)

Lecture 6

12

Name and Type


PLANE:
Type: PLANE
Name: Top
Top Plane,
Plane , Front
Front Plane,
Plane , Right
Right Plane,
Plane ,
user defined name, (empty name)

SKETCH ENTITY
Type: "SKETCHSEGMENT
Name: SketchArc,
k
h
SketchLine,
k
h
SketchEllipse,
k
h ll
SketchParabola, SketchSpline, user defined name

Lecture 6

13

Selection set line.swp

Selection Set Priority


Select
Select the line based on the coordinate if general name is used
boolstatus = Part.Extension.SelectByID2(SketchLine,
"SKETCHSEGMENT", 0, 0, -5, False, 0, Nothing, 0)
Select line1 when name line1 and the point on the line1
boolstatus = Part.Extension.SelectByID2("Line1",
"SKETCHSEGMENT",, 0,, 0,, -5,, False,, 0,, Nothing,
g, 0))

Select line2 when name is line2 albeit the point on the line1
boolstatus = Part
Part.Extension.SelectByID2(
Extension SelectByID2("Line2"
Line2 ,
"SKETCHSEGMENT", 0, 0, -5, False, 0, Nothing, 0)
*Highest
g
p
priority:
y specific
p
name
Lecture 6

14

Setting the 2D sketch plane


Orthogonal
O th
l sketch
k t h 2D sketch
k t h plane
l
Select the plane: either front, top or right plane
Set the
h sketch
k
h plane
l
Set
Set the top plane as sketch plane
boolstatus = Part.Extension.SelectByID2("Top Plane",
"PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part SketchManager InsertSketch True
Part.SketchManager.InsertSketch

Lecture 6

15

2D Sketch Plane: Face


boolstatus = Part.Extension.SelectByID2("",
"FACE", 1, 1, 0, False, 0, Nothing, 0)
Part.SketchManager.InsertSketch True

Lecture 6

16

Plane 3p.swp

Non-orthogonal 2D sketch plane


P
Procedure
d
using
i
S
SolidWorks
lidW k
1. Set the sketch using 3DSketch
2. Draw the 3 points using x,y,z
3. Insert the reference geometry plane
using the points
4. Activate the plane

Lecture 6

17

Step 1 and 2
1
1.

S t the
Set
th 3D sketch
k t h
Part.Insert3DSketch2 True

2.

Draw the 3 points


Set pt1 = Part.CreatePoint2(0, 0, 0)
Set pt2 = Part.CreatePoint2(10, 20, 0)
Set pt3 = Part.CreatePoint2(40, 20, 30)

Lecture 6

18

Step 3
3 Insert the geometry plane
3.
a. Select the points

Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("",
"EXTSKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("",
"EXTSKETCHPOINT"
EXTSKETCHPOINT , 10,
10 20,
20 0,
0 True,
True 0,
0 Nothing,
Nothing 0)
boolstatus = Part.Extension.SelectByID2("",
"EXTSKETCHPOINT", 40, 20, 30, True, 0, Nothing,
0)

b. Draw the plane

Part.CreatePlaneThru3Points3 True
Lecture 6

19

Step 4
4 Activate
4.
A ti t the
th plane
l
boolstatus = Part
Part.Extension.SelectByID2(RefPlane,
Extension SelectByID2(RefPlane
"PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.SketchManager.InsertSketch True

Lecture 6

20

Anda mungkin juga menyukai