- UID
- 10484
- 积分
- 0
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-9-26
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2002-12-16 13:18:13
|
显示全部楼层
Sub populateOfficeDatabase()
' Variables for AutoCAD objects
Dim objPoly As AutoCAD.AcadLWPolyline
Dim objText As AutoCAD.AcadText
Dim polySel As AutoCAD.AcadSelectionSet
' Variables for ADO objects
Dim rsSpaces As New ADODB.Recordset
Dim rsUseTypes As New ADODB.Recordset
Dim rsEmployee As New ADODB.Recordset
Dim rsDepartment As New ADODB.Recordset
' Variables for DCO objects
Dim LinkT As CAO.LinkTemplate
Dim keys As New CAO.KeyValues
Dim keyval As New CAO.KeyValue
Dim objLink As CAO.link
' Other variables
Dim wsPath As String
Dim useType As String
Dim DeptName(1 To 6) As String
Dim i As Long
' Store the department names in an array
DeptName(1) = "Civil Engineering"
DeptName(2) = "Architecture"
DeptName(3) = "Planning"
DeptName(4) = "Landscape Architecture"
DeptName(5) = "Surveying"
DeptName(6) = "Admin"
' Verify that the SPACES link template exists
On Error Resume Next
Set LinkT = getLinkTemplate(SpacesLink)
If Err <> 0 Then
MsgBox "You must first create a Link Template named '" & _
SpacesLink & "'" & _
vbCrLf & "based on the SPACES_QUERY table (SPACE_ID field)", _
vbOKOnly, "Office Example"
Exit Sub
End If
On Error GoTo 0
' Verify that the user really wants to do this
If MsgBox("This will clear the OFFICE database " & _
"and delete all links. Do you want to continue?", _
vbYesNo, "Office Example") = vbNo Then
Exit Sub
End If
' Delete all links associated with this link template
ThisDrawing.Utility.Prompt "Deleting links..." & vbCrLf
For Each objLink In getDbConnect().GetLinks(LinkT)
objLink.Delete
Next
' Open the database
openConnection SpacesLink
' Delete all rows from all tables
' (in case we run this macro more than once)
ThisDrawing.Utility.Prompt "Clearing the database..." & vbCrLf
getAdoConnection().Execute "delete from employee"
getAdoConnection().Execute "delete from spaces"
getAdoConnection().Execute "delete from use_types"
getAdoConnection().Execute "delete from department"
' Open the Recordset objects
rsSpaces.Open "SPACES", getAdoConnection(), _
adOpenDynamic, adLockOptimistic
rsUseTypes.Open "USE_TYPES", getAdoConnection(), _
adOpenDynamic, adLockOptimistic
rsEmployee.Open "EMPLOYEE", getAdoConnection(), _
adOpenDynamic, adLockOptimistic
rsDepartment.Open "DEPARTMENT", getAdoConnection(), _
adOpenDynamic, adLockOptimistic
' Iterate through the space polygons and populate the database
Set polySel = getSpacePolylineSelection()
For Each objPoly In polySel
' Find a text object inside the polygon
Set objText = getTextInsidePolygon(objPoly)
If Not objText Is Nothing Then
' Determine what the use of the space is. If the polygon
' color is set, then it's an office and the text is the
' employee name or "Vacant". Otherwise use the text value
' found inside the polygon as the use type.
useType = "Office"
If objPoly.Color = acByLayer Then
useType = objText.TextString
If useType = "Vacant" Then
useType = "Office"
End If
End If
' Add the use type to the USE_TYPES table if it does not exist
rsUseTypes.Find "TYPE_NAME='" & useType & _
"'", , , adBookmarkFirst
If rsUseTypes.EOF Then
rsUseTypes.AddNew
rsUseTypes!TYPE_NAME = useType
rsUseTypes.Update
End If
' Add a new row to the spaces table
rsSpaces.AddNew
rsSpaces!TYPE_ID = rsUseTypes!TYPE_ID
rsSpaces.Update
' Set up the key value for the link
keys.Clear
keyval.Value = rsSpaces!SPACE_ID
keys.Add keyval
' Create the link on the polygon
Set objLink = LinkT.CreateLink(objPoly.ObjectID, keys)
' This is an occupied office...
If objText.Color = acByLayer _
And objPoly.Color <> acByLayer Then
' Add the department name to the DEPARTMENT
' table if necessary
rsDepartment.Find "DEPT_NAME='" & _
DeptName(objPoly.Color) & "'", , , adBookmarkFirst
If rsDepartment.EOF Then
rsDepartment.AddNew
rsDepartment!DEPT_NAME = DeptName(objPoly.Color)
rsDepartment.Update
End If
' Split the first and last name by finding the position
' of the separating space
i = InStr(1, objText.TextString, " ", vbTextCompare)
' Add a new row to the employee table and populate it
rsEmployee.AddNew
rsEmployee!FIRST_NAME = Left(objText.TextString, i - 1)
rsEmployee!LAST_NAME = Mid(objText.TextString, i + 1)
rsEmployee!DEPT_ID = rsDepartment!DEPT_ID
rsEmployee!SPACE_ID = rsSpaces!SPACE_ID
rsEmployee.Update
End If
ThisDrawing.Utility.Prompt objText.TextString
End If
Next
' Close the recorset objects
rsSpaces.Close
rsUseTypes.Close
rsEmployee.Close
rsDepartment.Close
' Release the polyline selection object
Set polySel = Nothing
End Sub
编译时老在keys.add keyval 处出错 |
|