laoxie_198 发表于 2007-10-23 19:45:00

[原创]从SQL数据库中提取数据生成CAD图形

<p>Sub xz(dww)<br/>Dim sjk As New ADODB.Connection<br/>Dim dw As New ADODB.Recordset<br/>Dim text As New ADODB.Recordset<br/>Dim ty As New ADODB.Recordset<br/>Dim tyname As New Recordset<br/>Dim tylj As New ADODB.Recordset<br/>Dim point As New ADODB.Recordset<br/>Dim pointtext As New ADODB.Recordset<br/>Dim sjklj, dwname, dwlj, yhm, tc, tc1, bh, tyxx As String<br/>yhm = "002"<br/>Dim x, y As Double<br/>Dim tylx As String<br/>sjklj = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=hbcad"<br/>sjk.Open sjklj '打开库连接<br/>'dwname = InputBox("请输入单位名称")<br/>dwzfcd = Len(dww)<br/>dwname = Trim(dww)<br/>dwlj = "select * from tyname where dwname&nbsp; = '" &amp; dwname &amp; "'"<br/>tyname.Open dwlj, sjk, adOpenDynamic, adLockBatchOptimistic<br/>If tyname.EOF Then<br/>&nbsp; MsgBox ("库中无此单位信息")<br/>&nbsp; End<br/>&nbsp; Else<br/>&nbsp;&nbsp;&nbsp; If tyname.Fields("sm") = "002" Then<br/>&nbsp;&nbsp;&nbsp; MsgBox ("本图形已经有用户使用")<br/>&nbsp;&nbsp;&nbsp; End<br/>&nbsp;&nbsp;&nbsp; Else<br/>ty.Open "select * from tysx where dwid in (select dwid from tyname where dwname like '%" &amp; dwname &amp; "%'and sm&lt;&gt;'002' )", sjk, adOpenDynamic, adLockBatchOptimistic<br/>Dim w As Double<br/>w = 1<br/>Do While Not ty.EOF<br/>tyxx = ty.Fields("linetype") '线性<br/>tjxx (tyxx)<br/>tylx = ty.Fields("tylx") '图元类型<br/>bh = ty.Fields("is")<br/>tc = ty.Fields("larer") '图层<br/>&nbsp; If tylx = "AcDbMText" Or tylx = "AcDbText" Then<br/>&nbsp; te ty.Fields("cadid"), ty.Fields("larer"), sjk '注记<br/>&nbsp;&nbsp;&nbsp; End If<br/>&nbsp; tylj.Open "select * from tylj where tyid=" &amp; ty.Fields("cadid") &amp; " order by xh asc", sjk, adOpenDynamic, adLockBatchOptimistic<br/>&nbsp; Do While Not tylj.EOF<br/>&nbsp; point.Open "select *from point where pointid=" &amp; tylj.Fields("pointid") &amp; " ", sjk, adOpenDynamic, adLockBatchOptimistic<br/>&nbsp;&nbsp; Select Case tylx<br/>&nbsp;&nbsp; 'Case "AcDbMText" Or "AcDbText"<br/>&nbsp;&nbsp; 'te ty.Fields("cadid"), ty.Fields("larer"), sjk '注记<br/>&nbsp;&nbsp;&nbsp; Case "AcDbPolyline" '是多段线时<br/>&nbsp;&nbsp;&nbsp;&nbsp; 'Dim ewlayer As AcadLayer<br/>&nbsp;&nbsp;&nbsp;&nbsp; 'Set ewlayer = ThisDrawing.Layers.Add(tc)<br/>&nbsp;&nbsp;&nbsp;&nbsp; 'ThisDrawing.ActiveLayer = ewlayer<br/>&nbsp;&nbsp;&nbsp; ThisDrawing.SendCommand "_pline" &amp; vbCr<br/>&nbsp;&nbsp;&nbsp; x = point.Fields("x")<br/>&nbsp;&nbsp;&nbsp; y = point.Fields("y")<br/>&nbsp;&nbsp;&nbsp; ThisDrawing.SendCommand x &amp; "," &amp; y &amp; vbCr<br/>&nbsp;&nbsp;&nbsp; Case "AcDbLine" '是line线时<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'Dim wlayer As AcadLayer<br/>&nbsp;&nbsp;&nbsp;&nbsp; 'Set wlayer = ThisDrawing.Layers.Add(tc)<br/>&nbsp;&nbsp;&nbsp;&nbsp; 'ThisDrawing.ActiveLayer = wlayer<br/>&nbsp;&nbsp;&nbsp; ThisDrawing.SendCommand "_line" &amp; vbCr<br/>&nbsp;&nbsp;&nbsp; x = point.Fields("x")<br/>&nbsp;&nbsp;&nbsp; y = point.Fields("y")<br/>&nbsp;&nbsp;&nbsp; ThisDrawing.SendCommand x &amp; "," &amp; y &amp; vbCr<br/>&nbsp;&nbsp;&nbsp; Case "AcDbBlockReference"<br/>&nbsp;&nbsp;&nbsp; block point, sjk<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp; End Select<br/>&nbsp; tylj.MoveNext<br/>&nbsp; point.Close<br/>&nbsp;Loop<br/>&nbsp;If tylx = "AcDbPolyline" Then<br/>&nbsp; If bh = "1" Then<br/>&nbsp;&nbsp; ThisDrawing.SendCommand "c" &amp; vbCr<br/>&nbsp;&nbsp; Else<br/>&nbsp;&nbsp; ThisDrawing.SendCommand "" &amp; vbCr<br/>&nbsp;&nbsp; End If<br/>&nbsp;Else<br/>&nbsp; If tylx = "AcDbLine" Then<br/>&nbsp; ThisDrawing.SendCommand "" &amp; vbCr<br/>&nbsp; End If<br/>&nbsp;End If<br/>&nbsp;&nbsp; Dim xzj As AcadSelectionSet<br/>&nbsp; ' If Not IsNull(ThisDrawing.SelectionSets.Item("s1")) Then<br/>&nbsp;&nbsp;&nbsp; 'Set xzj = ThisDrawing.SelectionSets.Item("s1")<br/>&nbsp;&nbsp;&nbsp; 'xzj.Delete<br/>&nbsp; 'End If<br/>&nbsp;' Set xzj = ThisDrawing.SelectionSets.Add("s1")<br/>&nbsp;' xzj.Select acSelectionSetLast<br/>&nbsp;' Dim lr As AcadEntity<br/>&nbsp;' For Each lr In xzj<br/>&nbsp; <br/>&nbsp; 'lr = xzj.Item(1)<br/>&nbsp; 'lr.layer = tc<br/>&nbsp; 'Next lr<br/>tjkzsj ty.Fields("cadid"), dwname, tc<br/>&nbsp;tylj.Close<br/>ty.MoveNext<br/>Loop<br/>ty.Close<br/>End If<br/>End If<br/>sjk.Execute "update tyname set sm='" &amp; yhm &amp; "' where dwname like '%" &amp; dwname &amp; "%'"<br/>MsgBox ("数据下载完毕")<br/>sjk.Close<br/>End<br/>End Sub<br/>Private Sub block(point, cn) '插入块<br/>Dim blockobject As AcadBlock<br/>Dim ljj As New ADODB.Recordset<br/>Dim blockname As String<br/>Dim zb(0 To 2) As Variant<br/>ljj.Open "select * from block where cadid in (select tyid from tylj where pointid= " &amp; point.Fields("pointid") &amp; ")", cn, adOpenDynamic, adLockBatchOptimistic<br/>If Not ljj.EOF Then<br/>blockname = ljj.Fields("name")<br/>zb(0) = point.Fields("x")<br/>zb(1) = point.Fields("Y")<br/>zb(2) = 0<br/>'Set bolckobject = ThisDrawing.ModelSpace.InsertBlock(zb, blockname, 1, 1, 1, 0)<br/>'ThisDrawing.ModelSpace.InsertBlock zb, blockname, ljj.Fields("xs"), ljj.Fields("ys"), ljj.Fields("zs"), 1<br/>&nbsp; 'ThisDrawing.SendCommand "-insert" &amp; vbCr<br/>&nbsp; ThisDrawing.SendCommand "-insert" &amp; vbCr &amp; blockname &amp; vbCr &amp; point.Fields("x") &amp; "," &amp; point.Fields("y") &amp; vbCr &amp; ljj.Fields("xs") &amp; vbCr &amp; ljj.Fields("ys") &amp; vbCr &amp; ljj.Fields("zs") &amp; vbCr &amp; ljj.Fields("jd") &amp; vbCr<br/>&nbsp; 'ThisDrawing.SendCommand blockname &amp; vbCr<br/>&nbsp; 'ThisDrawing.SendCommand point.Fields("y") &amp; "," &amp; point.Fields("x") &amp; vbCr<br/>&nbsp; 'ThisDrawing.SendCommand ljj.Fields("xs") &amp; vbCr<br/>&nbsp;' ThisDrawing.SendCommand ljj.Fields("ys") &amp; vbCr<br/>&nbsp;' ThisDrawing.SendCommand ljj.Fields("zs") &amp; vbCr<br/>&nbsp; 'ThisDrawing.SendCommand 0 &amp; vbCr<br/>&nbsp; 'ThisDrawing.SendCommand "" &amp; vbCr<br/>&nbsp; <br/>End If<br/>ljj.Close<br/>End Sub<br/>Private Sub te(cadid, tc, sjk) '下载注记<br/>Dim jdzh As Double</p><p>&nbsp; Dim newlayer As AcadLayer<br/>&nbsp; Set newlayer = ThisDrawing.Layers.Add(tc)<br/>&nbsp; ThisDrawing.ActiveLayer = newlayer<br/>&nbsp;Dim text As New ADODB.Recordset<br/>&nbsp;Dim pointtext As New ADODB.Recordset<br/>&nbsp;text.Open "select * from tytext where cadid=" &amp; cadid &amp; "", sjk, adOpenDynamic, adLockBatchOptimistic<br/>&nbsp;pointtext.Open "select *from point where pointid in (select pointid from tytext where cadid=" &amp; cadid &amp; ")", sjk, adOpenDynamic, adLockBatchOptimistic<br/>&nbsp;&nbsp;&nbsp; jdzh = text.Fields("zjjd") / 0.017453292 '注记角度<br/>&nbsp;&nbsp;&nbsp; ThisDrawing.SendCommand "_text" &amp; vbCr<br/>&nbsp;&nbsp;&nbsp; ThisDrawing.SendCommand pointtext.Fields("x") &amp; "," &amp; pointtext.Fields("y") &amp; vbCr<br/>&nbsp;&nbsp;&nbsp; ThisDrawing.SendCommand text.Fields("ztdx") &amp; vbCr<br/>&nbsp;&nbsp;&nbsp; ThisDrawing.SendCommand jdzh &amp; vbCr<br/>&nbsp;&nbsp;&nbsp; ThisDrawing.SendCommand text.Fields("nr") &amp; vbCr<br/>&nbsp;&nbsp;&nbsp; ThisDrawing.SendCommand "" &amp; vbCr<br/>&nbsp;&nbsp;&nbsp; text.Close<br/>&nbsp; pointtext.Close</p><p>End Sub<br/>Private Sub tjkzsj(cadid, dwname, tc) '将新增的图元添加上扩展数据<br/>'On Error Resume Next<br/>wj = "d:\a.txt"<br/>Open wj For Append As #1<br/>Dim ty As AcadEntity<br/>Dim layer As AcadLayer<br/>Set layer = ThisDrawing.Layers.Add(tc)<br/>Dim i As Long<br/>i = ThisDrawing.ModelSpace.Count - 1<br/>Set ty = ThisDrawing.ModelSpace.Item(i)</p><p>'Dim ssget As AcadSelectionSet<br/>'On Error Resume Next<br/>'If Not IsNull(ThisDrawing.SelectionSets.Item("xzj")) Then<br/>&nbsp;'Set ssget = ThisDrawing.SelectionSets.Item("xzj")<br/>&nbsp;'ssget.Delete<br/>'End If<br/>'Set ssget = ThisDrawing.SelectionSets.Add("xzj")<br/>'ssget.Select acSelectionSetLast<br/>'For Each ty In ssget<br/>ty.layer = tc<br/>ty.Update<br/>Dim datatype(0 To 7) As Integer<br/>Dim data(0 To 7) As Variant<br/>datatype(0) = 1001: data(0) = "xdata"<br/>datatype(1) = 1000: data(1) = dwname<br/>datatype(2) = 1003: data(2) = "0"<br/>datatype(3) = 1040: data(3) = 1.232<br/>datatype(4) = 1041: data(4) = cadid<br/>datatype(5) = 1070: data(5) = 5656<br/>datatype(6) = 1071: data(6) = 32332<br/>datatype(7) = 1042: data(7) = 10<br/>ty.SetXData datatype, data<br/>ThisDrawing.Application.Update<br/>Dim xtype As Variant<br/>Dim xdata As Variant<br/>ty.GetXData "", xtpye, xdata<br/>Write #1, tc, xdata(4)<br/>'Next ty<br/>'Write #1, tc, xdata(4)<br/>Close #1<br/>'kckzsj ty<br/>'ssget.Delete<br/>End Sub</p><p>Private Sub kckzsj(ty As AcadEntity)<br/>Dim xtype As Variant<br/>Dim xdata As Variant<br/>ty.GetXData "", xtpye, xdata<br/>End Sub</p><p><br/>Private Sub tjtc(t As String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim wlayer As AcadLayer<br/>&nbsp;&nbsp;&nbsp;&nbsp; Set wlayer = ThisDrawing.Layers.Add(t)<br/>&nbsp;&nbsp;&nbsp;&nbsp; ThisDrawing.ActiveLayer = wlayer<br/>'Dim aa As AcadEntity<br/>'Dim ly As AcadLayer<br/>'Set ly = ThisDrawing.Layers.Add(t)<br/>'Dim ll As String<br/>'Dim l As Integer<br/>'i = ThisDrawing.ModelSpace.Count - 1<br/>'If i &lt; 0 Then<br/>'i = 0<br/>'End If<br/>'Set aa = ThisDrawing.ModelSpace.Item(i)<br/>'ll = aa.layer<br/>'aa.layer = t<br/>'MsgBox (ll)<br/>End Sub<br/>Private Sub tjxx(t As String)<br/>t = Trim(t)<br/>'ThisDrawing.SendCommand "_linetype" &amp; vbCr &amp; s &amp; vbCr &amp; t &amp; vbCr &amp; "" &amp; vbCr &amp; "" &amp; vbCr<br/>&nbsp;&nbsp;&nbsp; 'ThisDrawing.SendCommand "s" &amp; vbCr<br/>&nbsp;&nbsp;&nbsp; 'ThisDrawing.SendCommand t &amp; vbCr<br/>&nbsp;&nbsp;&nbsp; 'ThisDrawing.SendCommand "" &amp; vbCr<br/>ThisDrawing.ActiveLinetype = ThisDrawing.Linetypes.Item(t)</p><p>End Sub</p><p><br/></p>
页: [1]
查看完整版本: [原创]从SQL数据库中提取数据生成CAD图形