You are on page 1of 68

:

2D
.


:




:
Word , Excel , PowerPoint , Photoshop etc

.



- :
:
)

( .
:



:
) ( File<Save Project As


) ( col
.
: :
Project < references
:

:
- ) (form

- ) (project Explorer
) (.

: . View

.
.


:
) (command Button ) ( +
:
.
caption
:
- :
:


Dim acadapp As AcadApplication
Dim anobg As Object
On Error Resume Next
'
)"Set acadapp = GetObject(, "autocad.application
If Err Then
Err.Clear
'
)"Set acadapp = CreateObject("autocad.application
If Err Then
MsgBox Err.Description
Exit Sub
End If
End If
acadapp.Visible = True

.
:

) ( .

.

F5 star
.


:
)
(

.
- :
acadapp.Quit

- :
Dim lineObj As AcadLine
Dim startPoint(0 To 2) As Double
Dim endPoint(0 To 2) As Double
' Define the start and end points for the line
startPoint(0) = 1#: startPoint(1) = 1#: startPoint(2) = 0#
endPoint(0) = 5#: endPoint(1) = 5#: endPoint(2) = 0#
' Create the line in model space
= Set lineObj
)acadapp.ActiveDocument.ModelSpace.AddLine(startPoint, endPoint
:
lineObj AcadLine


startPoint endPoint Double
.
X,Y,Z
Model pace
AddLine .
Set lineObj = acadapp.ActiveDocument.ModelSpace.AddLine(startPoint,
)endPoint
:

Msgbox lineObj.Angle

Msgbox lineObj.Length

Msgbox lineObj.startPoint

Msgbox lineObj.endPoint

Msgbox lineObj.Layer
Msgbox lineObj.Linetype


.

:
TextBOX Label NAME :
X0,Y0,Z0
X1,Y1,Z1
caption Label
:

:
Dim lineObj As AcadLine
Dim startPoint(0 To 2) As Double
Dim endPoint(0 To 2) As Double
'Define the start and end points for the line
startPoint(0) = Val(X0.Text): startPoint(1) = Val(Y0.Text): startPoint(2)
= Val(Z0.Text)
endPoint(0) = Val(X1.Text): endPoint(1) = Val(Y1.Text): endPoint(2) =
Val(Z1.Text)
' Create the line in model space
Set lineObj = acadapp.ActiveDocument.ModelSpace.AddLine(startPoint,
endPoint)

Val Name X0 Val(X0.Text)



:

.

) '( .
- :
Dim pointObj As AcadPoint
Dim location(0 To 2) As Double
' Define the location of the point
location(0) = 5#: location(1) = 5#: location(2) = 0#
' Create the point
= Set pointObj
)acadapp.ActiveDocument.ModelSpace.AddPoint(location

:
pointObj AcadPoint

AddPoint Model Space
: .
:
acadapp.ActiveDocument.SetVariable "PDMODE", 3
PDMODE 3
:

:
acadapp.ActiveDocument.SetVariable "PDSIZE", 20
PDSIZE 20
:

- : circle

centerpoint Radius
:


Dim circleObj As AcadCircle
Dim centerPoint(0 To 2) As Double
Dim radius As Double
' Define the circle
centerPoint(0) = 0#: centerPoint(1) = 0#: centerPoint(2) = 0#
radius = 5#
' Create the Circle object in model space
= Set circleObj
)acadapp.ActiveDocument.ModelSpace.AddCircle(centerPoint, radius
:
circleObj AcadCircle
AddCircle . Model Space
:

.
- : Arc

:


:
AngleInRadian = tAngleInDegree * 3.141592 / 180
Dim arcObj As AcadArc
Dim centerPoint(0 To 2) As Double
Dim radius As Double
Dim startAngleInDegree As Double
Dim endAngleInDegree As Double
' Define the circle
centerPoint(0) = 0#: centerPoint(1) = 0#: centerPoint(2) = 0#
radius = 5#
startAngleInDegree = 10#
endAngleInDegree = 230#
' Convert the angles in degrees to angles in radians


Dim startAngleInRadian As Double
Dim endAngleInRadian As Double
startAngleInRadian = startAngleInDegree * 3.141592 / 180#
endAngleInRadian = endAngleInDegree * 3.141592 / 180#
' Create the arc object in model space
Set arcObj =
acadapp.ActiveDocument.ModelSpace.AddArc(centerPoint, radius,
startAngleInRadian, endAngleInRadian)

: Polyline -

.
.
Polyline

:
: Polyline -
Dim plineObj As AcadPolyline
Dim points(0 To 14) As Double
' Define the 2D polyline points
points(0) = 1: points(1) = 1: points(2) = 0
points(3) = 1: points(4) = 2: points(5) = 0
points(6) = 2: points(7) = 2: points(8) = 0
points(9) = 3: points(10) = 2: points(11) = 0
points(12) = 4: points(13) = 4: points(14) = 0


' Create a lightweight Polyline object in model space
Set plineObj =
acadapp.ActiveDocument.ModelSpace.AddPolyline(points)
:
plineObj.Closed=True
: Polyline -
:
plineObj.Type = acCubicSplinePoly
plineObj.Type = acFitCurvePoly
plineObj.Type = acQuadSplinePoly
.
: Polyline -
Polyline

:
Polyline
Dim plineObj As AcadLWPolyline
Dim points(0 To 11) As Double
' Define the 2D polyline points
points(0) = 1: points(1) = 1
points(2) = 1: points(3) = 2
points(4) = 2: points(5) = 2
points(6) = 3: points(7) = 2
points(8) = 4: points(9) = 4
points(10) = 4: points(11) = 1
' Create a lightweight Polyline object in model space
Set plineObj =
acadapp.ActiveDocument.ModelSpace.AddLightWeightPolyline(points)


' Find the bulge of the third segment
Dim currentBulge As Double
)currentBulge = plineObj.GetBulge(3
plineObj.SetBulge 3, -0.5
plineObj.Update
) (3 ) (-0.5



- : text
Dim textObj As AcadText
Dim textString As String
Dim insertionPoint(0 To 2) As Double
Dim height As Double


' Define the text object
textString = "Hello, World."
insertionPoint(0) = 2: insertionPoint(1) = 2: insertionPoint(2)= 0
height = 0.5
' Create the text object in model space
Set textObj =
acadapp.ActiveDocument.ModelSpace.AddText(textString,
insertionPoint, height)
AcadText textObj
insertionPoint height textString

: Mtext
Dim MTextObj As AcadMText
Dim corner(0 To 2) As Double
Dim width As Double
Dim text As String
corner(0) = 0#: corner(1) = 10#: corner(2) = 0#
width = 10
text = ""
' Creates the mtext Object
Set MTextObj =
acadapp.ActiveDocument.ModelSpace.AddMText(corner, width, text)
:
: new Document
Dim object As AcadDocument


Set object = acadapp.Application.Documents.Add

.
: close Document -
acadapp.ActiveDocument.Close

Dim DOC As AcadDocument
' If there are no open documents, then exit
If Documents.count = 0 Then
MsgBox ""!
Exit Sub
End If
' Close all open documents
For Each DOC In Documents
If MsgBox(" : " & DOC.WindowTitle,
vbYesNo & vbQuestion) = vbYes Then
If DOC.FullName <> "" Then
DOC.Close
Else
MsgBox DOC.name & " ."
End If
End If
Next
: Layer -
: (ABC) -
Dim layerObj As AcadLayer


)"Set layerObj = acadapp.Application.Layers.Add("ABC
- :
:
Dim color As AcadAcCmColor
= Set color
)"AcadApplication.GetInterfaceObject("AutoCAD.AcCmColor.16
)Call color.SetRGB(80, 100, 244
layerObj.TrueColor = color
)Call color.SetRGB(80, 100, 244
.
: plineObj :
plineObj.color=acBlue
plineObj.color=acRed
) = (
- :
:
layerObj.delete
- Command
- :
& acadapp.ActiveDocument.SendCommand "_Circle" & vbCr
"2,2,0" & vbCr & "4" & vbCr
acadapp.ActiveDocument.Regen acAllViewports
:
" "_Circle Command vbCr
Enter


vbCr
- : Zoom
& "acadapp.ActiveDocument.SendCommand "_zoom" & vbCr & "a
vbCr
' Refresh view
acadapp.ActiveDocument.Regen acAllViewports
All
" "a
:
ZoomAll

ZoomExtents


Window ..
: Zoom
' ZoomWindow
Dim point1(0 To 2) As Double
Dim point2(0 To 2) As Double
point1(0) = 1.3: point1(1) = 7.8: point1(2) = 0
point2(0) = 13.7: point2(1) = -2.6: point2(2) = 0
ZoomWindow point1, point2
' ZoomScaled
Dim scalefactor As Double
Dim scaletype As Integer
scalefactor = 2
scaletype = acZoomScaledRelative


ZoomScaled scalefactor, scaletype
'ZoomCenter
Dim zcenter(0 To 2) As Double
Dim magnification As Double
zcenter(0) = 3: zcenter(1) = 3: zcenter(2) = 0
magnification = 10
zoomcenter zcenter, magnification






.
:
:
- ) (
:


:

.

.... .
:
Dim newsset As AcadSelectionSet
= Set newsset
)"acadapp.ActiveDocument.SelectionSets.Add("TEST_SELECTIONSET
newsset.SelectOnScreen
Dim obj3 As AcadEntity
For Each obj3 In newsset


If obj3.ObjectName = "AcDbLine" Then
Msgbox obj3.Length
MsgBox obj3.angle
End If
Next obj3
"acadapp.ActiveDocument.SelectionSets.Item("TEST_SELECTIONSET
).Delete

:
newsset TEST_SELECTIONSET

Enter



: ... -
For Each obj3 In newsset
Msgbox obj3. Name
Next obj3
:
-
For Each obj3 In newsset
obj3.color = acGreen


Next obj3
: -
Msgbox newsset.count
: -
For Each obj3 In newsset
obj3.delete
Next obj3
:
Get
:


Dim acadline As acadline
Dim acadline1 As acadline
Dim newsset As AcadSelectionSet
Set newsset =
("acadapp.ActiveDocument.SelectionSets.Add("TEST_SELECTIONSET
newsset.SelectOnScreen
Dim returnPnt1 As Variant
returnPnt1 = acadapp.ActiveDocument.Utility.GetPoint
( " :")
Dim obj As acadline
For Each obj In newsset


Set acadline =
acadapp.ActiveDocument.ModelSpace.AddLine(obj.startPoint,
(returnPnt1
Set acadline1 =
acadapp.ActiveDocument.ModelSpace.AddLine(obj.endPoint,
(returnPnt1
obj.Delete
Next obj
acadapp.ActiveDocument.SelectionSets.Item("TEST_SELECTIONSET"
).Delete
:
:


:
Dim ssetObj As AcadSelectionSet
Set ssetObj =
acadapp.ActiveDocument.SelectionSets.Add("TEST_SSET")
ssetObj.SelectOnScreen
Dim Intpts As Variant
Intpts = ssetObj.Item(0).IntersectWith(ssetObj.Item(1), acExtendNone)
Dim I As Integer, j As Integer, k As Integer
Dim str As String


If VarType(Intpts) <> vbEmpty Then
For I = LBound(Intpts) To UBound(Intpts)
str = "Intersection Point[" & k & "] is: " & Intpts(j) & "," &
Intpts(j + 1) & "," & Intpts(j + 2)
MsgBox str, , "IntersectWith Example"
str = ""
I=I+2
j=j+3
k=k+1
Next
End If
acadapp.ActiveDocument.SelectionSets.Item("TEST_SSET").Delete

:

Dim noOfObjects As Integer
Dim angleToFill As Double
Dim basePnt(0 To 2) As Double
angleToFill = 3.14

' 180 degrees

basePnt(0) = 4#: basePnt(1) = 4#: basePnt(2) = 0#


Dim ssetObj As AcadSelectionSet
Set ssetObj =
acadapp.ActiveDocument.SelectionSets.Add("TEST_SSET")
ssetObj.SelectOnScreen
noOfObjects = acadapp.ActiveDocument.Utility.GetInteger("
: ")
Dim retObj As Variant
retObj = ssetObj.Item(0).ArrayPolar(noOfObjects, angleToFill,
basePnt)
acadapp.ActiveDocument.SelectionSets.Item("TEST_SSET").Delete

:



:
-
-

:
- Add-In <visual data manager

col
.

col

Add Field
:
Name a
Type Double ok
Add Field
. close

Build the Table




Data :
DatabaseName
Recordsource
:
:

:
.


.
-

Label textbox :

textbox Data1 Datasource a DataField


textbox Data1 Datasource b DataField
: :

Click :
On Error Resume Next
Data1.Recordset.AddNew


.
Click :
On Error Resume Next
Data1.UpdateRecord
Data1.Recordset.Bookmark = Data1.Recordset.LastModified

:


.


:




: Click
Dim acadapp As AcadApplication
Dim anobg As Object
On Error Resume Next
Set acadapp = GetObject(, "autocad.application")
If Err Then
Err.Clear
Set acadapp = CreateObject("autocad.application")
If Err Then
MsgBox Err.Description
Exit Sub
End If
End If
Dim starP(0 To 2) As Double
Dim endP(0 To 2) As Double
Dim plineObj As AcadPolyline
Dim I As Integer
Dim points(0 To 14) As Double
If Data1.Recordset.RecordCount = 0 Then Exit Sub
Data1.Recordset.MoveFirst


Dim n As Integer
n=0
For I = 0 To Data1.Recordset.RecordCount - 1
points(0) = 0: points(1) = 0: points(2) = 0
points(3) = Val(Text1.Text): points(4) = 0: points(5) = 0
points(6) = Val(Text1.Text): points(7) = Val(Text2.Text):
points(8) = 0
points(9) = 0: points(10) = Val(Text2.Text): points(11) = 0
points(12) = 0: points(13) = 0: points(14) = 0
Set plineObj =
acadapp.ActiveDocument.ModelSpace.AddPolyline(points)
starP(0) = 0: starP(1) = 0: starP(2) = 0
endP(0) = n: endP(1) = 0: endP(2) = 0
plineObj.Move starP, endP
n = n + Val(Text1.Text) + 5
Data1.Recordset.MoveNext
Next I
:

Polyline
Integer n
x
For Next

( 0,0,0)


n
..


:
:
Private Sub Form_Initialize()
Dim s As String
s = App.Path
If Right(s, 1) <> "\" Then s = s + "\"
s = s + "col.mdb"
Me.Data1.DatabaseName = s
Me.Refresh
End Sub
Form_Initialize

:
: -
Dim acadapp As AcadApplication
Set acadapp = GetObject(, "autocad.application")
Set acadapp = CreateObject("autocad.application")
Dim polarPnt As Variant
Dim angle As Double
Dim distance As Double
Dim returnPnt As Variant
Dim lineObj As AcadLine
Dim obj As AcadEntity
Dim newsset As AcadSelectionSet


Set newsset =
acadapp.ActiveDocument.SelectionSets.Add("TEST_SELECTI
ONSET")
newsset.SelectOnScreen
For Each obj In newsset
If obj.ObjectName = "AcDbLine" Then
returnPnt = acadapp.ActiveDocument.Utility.GetPoint(, "
: ")
angle = obj.angle
distance = obj.Length
polarPnt =
acadapp.ActiveDocument.Utility.PolarPoint(returnPnt, angle,
distance)
End If
Next obj
Set lineObj =
acadapp.ActiveDocument.ModelSpace.AddLine(returnPnt,
polarPnt)
acadapp.ActiveDocument.SelectionSets.Item("TEST_SELECTI
ONSET").Delete
ZoomExtents

: -
Dim acadapp As AcadApplication
Set acadapp = GetObject(, "autocad.application")
Set acadapp = CreateObject("autocad.application")
Dim objEnt As AcadPoint
Dim varPick As Variant
Dim strType As String
Dim intType As Integer
Dim dblSize As Double
On Error Resume Next
With acadapp.ActiveDocument.Utility
'' get the pdmode center type
.InitializeUserInput 1, "Dot None Cross X Tick"


strType = .GetKeyword(vbCr & "Center type
[Dot/None/Cross/X/Tick]: ")
If Err Then Exit Sub
Select Case strType
Case "Dot": intType = 0
Case "None": intType = 1
Case "Cross": intType = 2
Case "X": intType = 3
Case "Tick": intType = 4
End Select
'' get the pdmode surrounding type
.InitializeUserInput 1, "Circle Square Both"
strType = .GetKeyword(vbCr & "Outer type
[Circle/Square/Both]: ")
If Err Then Exit Sub
'Figure 8 - 12#
'The Point object
'Figure 8-13. Various
'Point styles
Select Case strType
Case "Circle": intType = intType + 32
Case "Square": intType = intType + 64
Case "Both": intType = intType + 96
End Select
'' get the pdsize
.InitializeUserInput 1, ""


dblSize = .GetDistance(, vbCr & "Enter a point size: ")
If Err Then Exit Sub
'' set the system varibles
With acadapp.ActiveDocument
.SetVariable "PDMODE", intType
.SetVariable "PDSIZE", dblSize
End With
'' now add points in a loop
Do
'' get user point for new vertex, use last pick as basepoint
varPick = .GetPoint(, vbCr & "Pick a point <exit>: ")
'' exit loop if no point picked
If Err Then Exit Do
'' add new vertex to pline at last offset
acadapp.ActiveDocument.ModelSpace.AddPoint varPick
Loop
End With




. Enter


: Dim acadapp As AcadApplication
Dim anobg As Object
On Error Resume Next
Set acadapp = GetObject(, "autocad.application")
If Err Then
Err.Clear
Set acadapp = CreateObject("autocad.application")
If Err Then
MsgBox Err.Description


Exit Sub
End If
End If
Dim varCenter As Variant
Dim varMove As Variant
Dim dblRadius As Double
Dim dblAngle As Double
Dim varRegions As Variant
Dim objEnts() As AcadEntity
On Error Resume Next
'' get input from user
With acadapp.ActiveDocument.Utility
varCenter = .GetPoint(, vbCr & "Pick the center point: ")
dblRadius = .GetDistance(varCenter, vbCr & "Indicate the radius:
")
dblAngle = .AngleToReal("180", acDegrees)
End With
'' draw the entities
With acadapp.ActiveDocument.ModelSpace
'' draw the outer region (circle)
ReDim objEnts(2)
Set objEnts(0) = .AddCircle(varCenter, dblRadius)
'' draw the inner region (semicircle)
Set objEnts(1) = .AddArc(varCenter, dblRadius * 0.5, 0, dblAngle)
Set objEnts(2) = .AddLine(objEnts(1).startPoint,
objEnts(1).endPoint)


'CHAPTER 8

DRAWING OBJECTS 169

'Figure 8-14. Region objects


'' create the regions
varRegions = .AddRegion(objEnts)
End With
'' get new position from user
varMove = acadapp.ActiveDocument.Utility.GetPoint(varCenter,
vbCr & _
"Pick a new location: ")
'' subtract the inner region from the outer
varRegions(1).Boolean acSubtraction, varRegions(0)
'' move the composite region to a new location
varRegions(1).Move varCenter, varMove



: Dim acadapp As AcadApplication
Dim anobg As Object
On Error Resume Next
'
Set acadapp = GetObject(, "autocad.application")
If Err Then
Err.Clear
' autocad
Set acadapp = CreateObject("autocad.application")
If Err Then
MsgBox Err.Description
Exit Sub
End If
End If
Dim dblPoints(5) As Double
Dim varStartPoint As Variant
Dim varEndPoint As Variant
Dim intLeaderType As Integer
Dim objAcadLeader As AcadLeader
Dim objAcadMtext As AcadMText
Dim strMtext As String
Dim intI As Integer
intLeaderType = acLineWithArrow


varStartPoint = acadapp.ActiveDocument.Utility.GetPoint(, _
"Select leader start point: ")
varEndPoint =
acadapp.ActiveDocument.Utility.GetPoint(varStartPoint, _
"Select leader end point: ")
For intI = 0 To 2
dblPoints(intI) = varStartPoint(intI)
dblPoints(intI + 3) = varEndPoint(intI)
Next
strMtext = InputBox("Notes:", "Leader Notes")
If strMtext = "" Then Exit Sub
' Create the text for the leader
Set objAcadMtext =
acadapp.ActiveDocument.ModelSpace.AddMText(varEndPoint, _
Len(strMtext) * acadapp.ActiveDocument.GetVariable("dimscale"),
strMtext)
' Flip the alignment direction of the text
If varEndPoint(0) > varStartPoint(0) Then
objAcadMtext.AttachmentPoint = acAttachmentPointMiddleLeft
Else
objAcadMtext.AttachmentPoint = acAttachmentPointMiddleRight
End If
objAcadMtext.InsertionPoint = varEndPoint
'Create the leader object
Set objAcadLeader =
acadapp.ActiveDocument.ModelSpace.AddLeader(dblPoints, _


objAcadMtext, intLeaderType)
objAcadLeader.Update


: Dim acadapp As AcadApplication
Dim anobg As Object
On Error Resume Next
'
Set acadapp = GetObject(, "autocad.application")
If Err Then
Err.Clear
' autocad
Set acadapp = CreateObject("autocad.application")


If Err Then
MsgBox Err.Description
Exit Sub
End If
End If
Dim varCenter As Variant
Dim dblRadius As Double
Dim dblAngle As Double
Dim objEnt As AcadHatch
Dim varOuter() As AcadEntity
Dim varInner() As AcadEntity
On Error Resume Next
'' get input from user
With acadapp.ActiveDocument.Utility
varCenter = .GetPoint(, vbCr & "Pick the center point: ")
dblRadius = .GetDistance(varCenter, vbCr & "Indicate the radius:
")
dblAngle = .AngleToReal("180", acDegrees)
End With
'' draw the entities With ThisDrawing.ModelSpace
'' draw the Outer loop (circle)
With acadapp.ActiveDocument.ModelSpace
ReDim varOuter(0)
Set varOuter(0) = .AddCircle(varCenter, dblRadius)
'' draw then Inner loop (semicircle)
ReDim varInner(1)


Set varInner(0) = .AddArc(varCenter, dblRadius * 0.5, 0, dblAngle)
Set varInner(1) = .AddLine(varInner(0).startPoint, _
varInner(0).endPoint)
'' create the Hatch object
Set objEnt = .AddHatch(acHatchPatternTypePreDefined,
"ANSI31", True)
'' append boundaries to the hatch
objEnt.AppendOuterLoop varOuter
objEnt.AppendInnerLoop varInner
'' evaluate and display hatched boundaries
objEnt.Evaluate
objEnt.Update
EndWith


: Dim acadapp As AcadApplication
Dim anobg As Object
On Error Resume Next
'
Set acadapp = GetObject(, "autocad.application")
If Err Then
Err.Clear
' autocad
Set acadapp = CreateObject("autocad.application")
If Err Then
MsgBox Err.Description
Exit Sub
End If
End If
Dim vInsertionPoint As Variant
Dim lNumberOfRows As Long
Dim lNumberOfColumns As Long
Dim dRowHeight As Double
Dim dColumnWidth As Double
Dim oTable As AcadTable
On Error Resume Next
With acadapp.ActiveDocument.Utility
vInsertionPoint = .GetPoint(, vbCr & " : ")
lNumberOfRows = .GetInteger(vbCr & " : ")
lNumberOfColumns = .GetInteger(vbCr & " : ")


dRowHeight = .GetReal(vbCr & " : ")
dColumnWidth = .GetReal(vbCr & " : ")
End With
If Err Then Exit Sub
Set oTable =
acadapp.ActiveDocument.ModelSpace.AddTable(vInsertionPoint,
_
lNumberOfRows, lNumberOfColumns, dRowHeight,
dColumnWidth)



: object.InsertRows Index, RowHeight, Rows
:
oTable.InsertRows 1, 0.25, 2

: object.InsertColumns Index, ColumnWidth, Columns


:
oTable.InsertColumns 1, 1.25, 2

: object.SetText Row, Column, Text


:
oTable.SetText 0, 0, ""


:

Help
F1
:

.
Methods .
:

:
AddLine Method
:

Example


:
ThisDrawing acadapp.ActiveDocument
acadapp .
.

.

) (

You might also like