laoxie_198 发表于 2007-6-15 17:44:00

[原创]cad图形写入SQL数据库实例

<p>Sub sc()<br/>Dim dwname, tymc As String<br/>Dim cn As New ADODB.Connection<br/>Dim rst As New ADODB.Recordset<br/>Dim poin As New ADODB.Recordset<br/>Dim tyl As New ADODB.Recordset<br/>Dim block As New ADODB.Recordset 'block表处理<br/>Dim klj, blj, lx, point, tylj, yhmm As String<br/>yhmm = ""<br/>Dim tysx As String<br/>Dim id, cadid, js, bh, dwsm, tyidh As Long<br/>Dim mytime As Date<br/>Dim mj As Double<br/>Dim dds, mm As Integer '图元计算<br/>dds = 0<br/>mm = 0<br/>Dim plzb As Variant '顶点坐标数组<br/>mytime = CDate(Date)<br/>Dim ob As AcadEntity<br/>For Each ob In ThisDrawing.ModelSpace<br/>If ob.layer = "图形单位名称" Then<br/>&nbsp; txmc = Trim$(ob.TextString)<br/>&nbsp; z = InStr(txmc, ";")<br/>&nbsp; tymc = Mid$(txmc, z + 1, (Len(txmc) - z))<br/>&nbsp; If Right$(tymc, 1) = "}" Then<br/>&nbsp; tymc = Mid$(tymc, 1, (Len(tymc) - 1))<br/>&nbsp; End If<br/>&nbsp; End If<br/>Next ob<br/>If tymc &lt;&gt; "" Then<br/>dwname = tymc<br/>point = "select * from point order by pointid desc"<br/>tylj = "select * from tylj where tyid in (select cadid from tysx where dwid in (select dwid from tyname where dwname=" &amp; dwname &amp; "))"<br/>blj = "select * from tysx where dwid in (select dwid from tyname where dwname= '" &amp; dwname &amp; "')"<br/>'klj = "provider=sqloledb.1;password=;persist security info=true;user id=sa;initial catalog=cad ;data source=huangbin" '库连接字符串<br/>klj = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=hbcad"<br/>cn.Open klj '连接数据库<br/>dwsm = 0<br/>rst.Open "select * from tyname order by dwid desc", cn, adOpenDynamic, adLockBatchOptimistic<br/>Do While Not rst.EOF<br/>&nbsp; dwsm = rst.Fields("dwid")&nbsp; '统计单位数目最大号<br/>&nbsp; GoTo 2<br/>&nbsp; 'rst.MoveNext<br/>Loop<br/>2: rst.Close<br/>rst.Open "select * from tyname where dwname='" &amp; dwname &amp; "'", cn, adOpenDynamic, adLockBatchOptimistic<br/>If rst.EOF Then<br/>&nbsp; mm = mm + 1<br/>&nbsp; cn.Execute "insert into tyname values(" &amp; dwsm + 1 &amp; ",'" &amp; dwname &amp; "','', " &amp; mytime &amp; ")"<br/>&nbsp; Else<br/>&nbsp; yhm = Trim(rst.Fields("sm"))<br/>&nbsp; If yhm = "" Then<br/>&nbsp;&nbsp; MsgBox "数据已经上传"<br/>&nbsp;&nbsp; End<br/>&nbsp; End If<br/>End If<br/>rst.Close<br/>Dim obj As AcadEntity<br/>Dim obj1 As AcadEntity<br/>Dim obj2 As AcadEntity<br/>Dim obj3 As AcadEntity<br/>Dim obj4 As AcadEntity<br/>Dim obj5 As AcadEntity 'text<br/>Dim obj6 As AcadEntity<br/>Dim obj7 As AcadEntity<br/>'rst.Open blj, cn, adOpenDynamic, adLockBatchOptimistic<br/>'----------以下删除库中多余的图元信息<br/>rst.Open blj, cn, adOpenDynamic, adLockBatchOptimistic<br/>kzsj obj, cn, dwname '将所有图元添加扩展数据<br/>findid dwname, cn<br/>cn.Execute "delete tylj from tylj where tyid not in (select cadid from tysx)"<br/>cn.Close<br/>'----------以上删除库中多余的图元信息<br/>For Each obj In ThisDrawing.ModelSpace<br/>cn.Open klj '<br/>Dim xtype As Variant<br/>Dim xdata As Variant<br/>obj.GetXData "", xtpye, xdata<br/>tyidh = xdata(4)</p><p>Set obj1 = obj<br/>Set obj2 = obj<br/>Set obj3 = obj<br/>Set obj4 = obj<br/>Set obj5 = obj<br/>Set obj6 = obj<br/>dds = dds + 1 '图元统计<br/>tysx = obj.ObjectName<br/>Dim i As Integer<br/>&nbsp;&nbsp;&nbsp; rst.Open "select * from tysx where&nbsp; cadid =" &amp; tyidh &amp; "", cn, adOpenStatic, adLockReadOnly<br/>&nbsp;&nbsp;&nbsp; If Not rst.EOF Then<br/>&nbsp;&nbsp;&nbsp; id = rst.Fields("cadid")<br/>&nbsp;&nbsp;&nbsp; cadid = rst.Fields("cadid")<br/>&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; rst.Close<br/>&nbsp;&nbsp;&nbsp; rst.Open "select * from tysx", cn, adOpenDynamic, adLockBatchOptimistic<br/>&nbsp;&nbsp;&nbsp; If cadid = xdata(4) Then '图元修改<br/>&nbsp;&nbsp;&nbsp; cn.Execute "update tysx set time=" &amp; mytime &amp; " where cadid=" &amp; tyidh &amp; ""<br/>&nbsp;&nbsp;&nbsp; poin.Open point, cn, adOpenDynamic, adLockBatchOptimistic '打开point表连接<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Select Case tysx<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case "AcDbPolyline": '是pl线时<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xgpline poin, rst, tylj, obj3, cn, xdata(4) '将值插入到tylj表中<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case "AcDbLine": '是l线时<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xgli poin, rst, tylj, obj4, cn, xdata(4) '将值插入到point表中<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case "AcDbMText"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mytex poin, tylj, obj6, cn, xdata(4)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case "AcDbText"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ytex poin, tylj, obj6, cn, xdata(4)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case "AcDbBlockReference" '块<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; blockxg point, tylj, obj, cn, xdata(4)<br/>&nbsp;&nbsp;&nbsp; End Select<br/>&nbsp;&nbsp;&nbsp; Else '图元新增<br/>&nbsp;&nbsp;&nbsp; If mm = 1 Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If obj.ObjectName = "AcDbPolyline" Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If obj.Closed = "True" Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cn.Execute "insert into tysx values (" &amp; tyidh &amp; ", '" &amp; tysx &amp; "', " &amp; mytime &amp; "," &amp; dwsm + 1 &amp; ",'" &amp; obj.layer &amp; "',1,'" &amp; obj.Linetype &amp; "','" &amp; obj.color &amp; "')"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cn.Execute "insert into tysx values (" &amp; tyidh &amp; ", '" &amp; tysx &amp; "', " &amp; mytime &amp; "," &amp; dwsm + 1 &amp; ",'" &amp; obj.layer &amp; "',0 ,'" &amp; obj.Linetype &amp; "','" &amp; obj.color &amp; "')"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cn.Execute "insert into tysx values (" &amp; tyidh &amp; ", '" &amp; tysx &amp; "', " &amp; mytime &amp; "," &amp; dwsm + 1 &amp; ",'" &amp; obj.layer &amp; "',0 ,'" &amp; obj.Linetype &amp; "','" &amp; obj.color &amp; "')" '将图元信息插入tysx表中<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; Else<br/>&nbsp;&nbsp;&nbsp; If obj.ObjectName = "AcDbPolyline" Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If obj.Closed = "True" Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cn.Execute "insert into tysx values (" &amp; tyidh &amp; ", '" &amp; tysx &amp; "', " &amp; mytime &amp; "," &amp; dwsm &amp; ",'" &amp; obj.layer &amp; "',1,'" &amp; obj.Linetype &amp; "','" &amp; obj.color &amp; "')"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cn.Execute "insert into tysx values (" &amp; tyidh &amp; ", '" &amp; tysx &amp; "', " &amp; mytime &amp; "," &amp; dwsm &amp; ",'" &amp; obj.layer &amp; "',0 ,'" &amp; obj.Linetype &amp; "','" &amp; obj.color &amp; "')"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cn.Execute "insert into tysx values (" &amp; tyidh &amp; ", '" &amp; tysx &amp; "', " &amp; mytime &amp; "," &amp; dwsm &amp; ",'" &amp; obj.layer &amp; "',0 ,'" &amp; obj.Linetype &amp; "')"&nbsp; '将图元信息插入tysx表中<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; poin.Open point, cn, adOpenDynamic, adLockBatchOptimistic '打开point表连接<br/>&nbsp;&nbsp;&nbsp; Select Case tysx<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case "AcDbPolyline": '是pl线时<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pline poin, tylj, obj2, cn, tyidh '将值插入到tylj表中<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case "AcDbLine": '是l线时<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; li poin, tylj, obj1, cn, tyidh '将值插入到point表中<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case "AcDbMText" '新增text'<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mtex poin, tylj, obj5, cn, tyidh<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case "AcDbText"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tex poin, tylj, obj5, cn, tyidh<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case "AcDbBlockReference" '块新增<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; blockadd point, tylj, obj, cn, tyidh<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case "AcDbPoint" '点<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pointad point, tylj, obj, cn, tyidh<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; End Select<br/>&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp; rst.Close<br/>&nbsp;rst.Open blj, cn, adOpenDynamic, adLockBatchOptimistic</p><p><br/>&nbsp;cn.Close<br/>Next obj<br/>'MsgBox ("上传完毕")<br/>Else<br/>MsgBox ("没有单位名称")<br/>End<br/>End If<br/>cn.Open , klj<br/>cn.Execute "update tyname set sm='" &amp; yhmm &amp; "' where dwname like '%" &amp; dwname &amp; "%'"<br/>MsgBox ("数据上传完毕")<br/>cn.Close<br/>End<br/>End Sub<br/>Private Sub ytex(point, ty1, obj6 As AcadEntity, cn, tyidh) 'text修改<br/>Dim zb As Variant<br/>Dim dh, zjjd As Double<br/>dh = 0<br/>Dim ztgd As Double<br/>zjjd = obj6.Rotation<br/>ztgd = obj6.Height<br/>zb = obj6.InsertionPoint<br/>txmc = Trim$(obj6.TextString)<br/>&nbsp; z = InStr(txmc, ";")<br/>&nbsp; tymc = Mid$(txmc, z + 1, (Len(txmc) - z))<br/>&nbsp;&nbsp;&nbsp; If Right$(tymc, 1) = "}" Then<br/>&nbsp; tymc = Mid$(tymc, 1, (Len(tymc) - 1))<br/>&nbsp; End If<br/>cn.Execute "update point set x=" &amp; zb(0) &amp; ",y=" &amp; zb(1) &amp; " from point where pointid in (select pointid from tytext where cadid=" &amp; tyidh &amp; " )"<br/>cn.Execute "update tytext set nr='" &amp; tymc &amp; "',layer='" &amp; obj6.layer &amp; "',ztdx=" &amp; ztgd &amp; " ,zjjd= " &amp; zjjd &amp; "from tytext where cadid=" &amp; tyidh &amp; ""<br/>cn.Execute "update tysx set color='" &amp; obj6.color &amp; "' from tysx where cadid=" &amp; tyidh &amp; ""</p><p>End Sub<br/>Private Sub mytex(point, ty1, obj As AcadEntity, cn, tyidh) '多行文字修改<br/>Dim zb As Variant<br/>Dim dh, zjjd As Double<br/>Dim width As DataTypeEnum<br/>dh = 0<br/>Dim ztgd As Double<br/>width = obj.width<br/>zjjd = obj.Rotation<br/>ztgd = obj.Height<br/>zb = obj.InsertionPoint<br/>txmc = Trim$(obj.TextString)<br/>&nbsp; z = InStr(txmc, ";")<br/>&nbsp; tymc = Mid$(txmc, z + 1, (Len(txmc) - z))<br/>&nbsp;&nbsp;&nbsp; If Right$(tymc, 1) = "}" Then<br/>&nbsp; tymc = Mid$(tymc, 1, (Len(tymc) - 1))<br/>&nbsp; End If<br/>cn.Execute "update point set x=" &amp; zb(0) &amp; ",y=" &amp; zb(1) &amp; " from point where pointid in (select pointid from tytext where cadid=" &amp; tyidh &amp; " )"<br/>cn.Execute "update tytext set nr='" &amp; tymc &amp; "',layer='" &amp; obj.layer &amp; "',ztdx=" &amp; ztgd &amp; " ,zjjd= " &amp; zjjd &amp; " , width =" &amp; width &amp; " from tytext where cadid=" &amp; tyidh &amp; ""<br/>cn.Execute "update tysx set color='" &amp; obj6.color &amp; "' from tysx where cadid=" &amp; tyidh &amp; ""<br/>End Sub</p><p>Private Sub tex(point, ty1, obj5 As AcadEntity, cn, tyidh)&nbsp; 'text新增<br/>Dim ztgd, zjjd As Double<br/>Dim wjwidth As Double<br/>ztgd = obj5.Height<br/>Dim zb As Variant<br/>Dim dh, ydh As Double<br/>dh = 0<br/>Dim pt As New ADODB.Recordset<br/>pt.Open "select * from point order by pointid desc", cn, adOpenDynamic, adLockBatchOptimistic<br/>If Not pt.EOF Then<br/>&nbsp;&nbsp; dh = pt.Fields("pointid")<br/>End If '计算点总数<br/>pt.Close<br/>zb = obj5.InsertionPoint<br/>zjjd = obj5.Rotation<br/>txmc = Trim$(obj5.TextString)<br/>&nbsp; z = InStr(txmc, ";")<br/>&nbsp; tymc = Mid$(txmc, z + 1, (Len(txmc) - z))<br/>&nbsp;&nbsp;&nbsp; If Right$(tymc, 1) = "}" Then<br/>&nbsp; tymc = Mid$(tymc, 1, (Len(tymc) - 1))<br/>&nbsp; End If<br/>&nbsp; cn.Execute "insert into tylj values( " &amp; tyidh &amp; ",1," &amp; dh + 1 &amp; ")" '图元连接<br/>pt.Open "select *from point where x=" &amp; zb(0) &amp; " and y=" &amp; zb(0) &amp; " ", cn, adOpenDynamic, adLockBatchOptimistic<br/>If Not pt.EOF Then<br/>&nbsp; ydh = pt.Fields("pointid")<br/>&nbsp; cn.Execute "insert into tytext values(" &amp; tyidh &amp; ",'" &amp; tymc &amp; "','" &amp; obj5.layer &amp; "'," &amp; ztgd &amp; ",ydh, " &amp; zjjd &amp; ")" '角度<br/>&nbsp; Else<br/>&nbsp; cn.Execute "insert into tytext values(" &amp; tyidh &amp; ",'" &amp; tymc &amp; "','" &amp; obj5.layer &amp; "'," &amp; ztgd &amp; "," &amp; dh + 1 &amp; ", " &amp; zjjd &amp; " ,0)"<br/>&nbsp; cn.Execute "insert into point values( " &amp; dh + 1 &amp; ",'' ," &amp; zb(0) &amp; "," &amp; zb(1) &amp; ",'')"<br/>End If</p><p>End Sub<br/>Private Sub mtex(point, ty1, obj As AcadEntity, cn, tyidh) '多行文字新增<br/>Dim ztgd, zjjd As Double<br/>Dim wjwidth As Double<br/>ztgd = obj.Height<br/>Dim zb As Variant<br/>Dim dh, ydh As Double<br/>dh = 0<br/>Dim pt As New ADODB.Recordset<br/>pt.Open "select * from point order by pointid desc", cn, adOpenDynamic, adLockBatchOptimistic<br/>If Not pt.EOF Then<br/>&nbsp;&nbsp; dh = pt.Fields("pointid")<br/>End If '计算点总数<br/>pt.Close<br/>wjwidth = obj.width<br/>zb = obj.InsertionPoint<br/>zjjd = obj.Rotation<br/>txmc = Trim$(obj.TextString)<br/>&nbsp; z = InStr(txmc, ";")<br/>&nbsp; tymc = Mid$(txmc, z + 1, (Len(txmc) - z))<br/>&nbsp;&nbsp;&nbsp; If Right$(tymc, 1) = "}" Then<br/>&nbsp; tymc = Mid$(tymc, 1, (Len(tymc) - 1))<br/>&nbsp; End If<br/>&nbsp; cn.Execute "insert into tylj values( " &amp; tyidh &amp; ",1," &amp; dh + 1 &amp; ")" '图元连接<br/>pt.Open "select *from point where x=" &amp; zb(0) &amp; " and y=" &amp; zb(0) &amp; " ", cn, adOpenDynamic, adLockBatchOptimistic<br/>If Not pt.EOF Then<br/>&nbsp; ydh = pt.Fields("pointid")<br/>&nbsp; cn.Execute "insert into tytext values(" &amp; tyidh &amp; ",'" &amp; tymc &amp; "','" &amp; obj.layer &amp; "'," &amp; ztgd &amp; ",ydh, " &amp; zjjd &amp; "," &amp; wjwidth &amp; ")" '角度<br/>&nbsp; Else<br/>&nbsp; cn.Execute "insert into tytext values(" &amp; tyidh &amp; ",'" &amp; tymc &amp; "','" &amp; obj.layer &amp; "'," &amp; ztgd &amp; "," &amp; dh + 1 &amp; ", " &amp; zjjd &amp; " ," &amp; wjwidth &amp; ")"<br/>&nbsp; cn.Execute "insert into point values( " &amp; dh + 1 &amp; ",'' ," &amp; zb(0) &amp; "," &amp; zb(1) &amp; ",'')"<br/>End If</p><p>End Sub<br/>Private Sub xgpline(point, rst, ty1, obj3 As AcadEntity, cn, tyidh) '原有pl线处理过程<br/>Dim plzb As Variant '顶点坐标数组<br/>Dim ss, dh As Integer<br/>Dim js As Integer<br/>Dim sql As String<br/>js = 0<br/>dh = 1<br/>Dim xw As New ADODB.Recordset<br/>sql = "select qq=sum(pointid) from point where pointid in (select pointid from tylj where tyid =" &amp; tyidh &amp; ") "<br/>xw.Open sql, cn, adOpenDynamic, adLockBatchOptimistic<br/>Do While Not xw.EOF '判断数据库里点数<br/>&nbsp;js = xw.Fields("qq")<br/>&nbsp;GoTo 4<br/>&nbsp;'xw.MoveNext<br/>&nbsp;Loop<br/>4:&nbsp; plzb = obj3.Coordinates<br/>&nbsp; ss = (UBound(plzb) + 1) / 2 '图形中本图元点数<br/>&nbsp; If js = ss Then<br/>&nbsp; Dim zb As Variant<br/>&nbsp; For i = 0 To ss - 1<br/>&nbsp;&nbsp; zb = obj3.Coordinate(i) '单个点坐标<br/>&nbsp;&nbsp; cn.Execute "update point set x=" &amp; zb(0) &amp; ",y=" &amp; zb(1) &amp; " from point where pointid in (select pointid from tylj where tyid=" &amp; tyidh &amp; " and xh=" &amp; dh &amp; ")"<br/>&nbsp;&nbsp; dh = dh + 1<br/>&nbsp; Next i<br/>&nbsp; End If<br/>&nbsp; point.Close<br/>&nbsp;End Sub<br/>Private Sub xgli(point, rst, tyl, obj4 As AcadEntity, cn, tyidh) '原有line线处理<br/>Dim i As Integer<br/>i = 0<br/>Dim pt1 As Variant<br/>Dim pt2 As Variant<br/>pt1 = obj4.StartPoint<br/>pt2 = obj4.EndPoint<br/>cn.Execute "update point set x=" &amp; pt1(0) &amp; ",y=" &amp; pt1(1) &amp; " from point where pointid in (select pointid from tylj where tyid=" &amp; tyidh &amp; " and xh=1)"<br/>cn.Execute "update point set x=" &amp; pt2(0) &amp; ",y=" &amp; pt2(1) &amp; " from point where pointid in (select pointid from tylj where tyid=" &amp; tyidh &amp; " and xh=2)"</p><p><br/>End Sub<br/>Private Sub blockxg(point, ty1, obj As AcadEntity, cn, tyidh) '原有块处理<br/>Dim zb As Variant<br/>Dim x, y, z As Integer<br/>Dim jd As Double<br/>Dim blockname As String<br/>x = obj.XScaleFactor<br/>y = obj.YScaleFactor<br/>z = obj.Zsxalefactor<br/>zb = obj.InsertionPoint<br/>jd = obj.Rotation * 57.28970065<br/>blockname = obj.name<br/>cn.Execute "update point set x=" &amp; zb(0) &amp; ",y=" &amp; zb(1) &amp; " from point where pointid in (select pointid from tylj where tyid=" &amp; tyidh &amp; ")"<br/>cn.Execute "update block set xs=" &amp; x &amp; " ,yx= " &amp; y &amp; ", zs=" &amp; z &amp; " ,jd=" &amp; jd &amp; " from block where cadid=" &amp; tyidh &amp; ""<br/>End Sub<br/>Private Sub blockadd(point, ty1, obj As AcadEntity, cn, tyidh) '新增块处理<br/>Dim block As New ADODB.Recordset<br/>Dim zb As Variant<br/>Dim dh, x, y, z As Integer<br/>Dim crjd As Double '插入角度<br/>x = obj.XScaleFactor<br/>y = obj.YScaleFactor<br/>z = obj.ZScaleFactor<br/>zb = obj.InsertionPoint<br/>crjd = obj.Rotation * 57.29578049</p><p>l = 1<br/>blockname = obj.name<br/>block.Open "select pointid from point order by pointid desc", cn, adOpenDynamic, adLockBatchOptimistic<br/>If Not block.EOF Then<br/>dh = block.Fields("pointid")<br/>Else<br/>dh = 0<br/>End If<br/>cn.Execute "insert into tylj values( " &amp; tyidh &amp; ",1," &amp; dh + 1 &amp; ")" '图元连接<br/>cn.Execute "insert into point values( " &amp; dh + 1 &amp; ",'' ," &amp; zb(0) &amp; "," &amp; zb(1) &amp; ",'')" '点表<br/>cn.Execute "insert into block values(" &amp; tyidh &amp; " ,'" &amp; blockname &amp; " '," &amp; x &amp; "," &amp; y &amp; ", " &amp; z &amp; " ," &amp; crjd &amp; "," &amp; dh + 1 &amp; ") " '块属性。</p><p><br/>End Sub<br/>Private Sub pointad(point, ty, obj As AcadEntity, cn, tyidh) '新增点的处理<br/>Dim zb As Variant<br/>Dim po As New ADODB.Recordset<br/>po.Open "select * from point order by pointid desc", cn, adOpenDynamic, adLockBatchOptimistic<br/>If Not po.EOF Then<br/>&nbsp;dh = po.Fields("pointid")<br/>&nbsp;Else<br/>&nbsp;dh = 0<br/>&nbsp;End If<br/>&nbsp;po.Close<br/>&nbsp;<br/>zb = obj.Coordinates<br/>cn.Execute "insert into tylj values( " &amp; tyidh &amp; ",1," &amp; dh + 1 &amp; ")" '图元连接<br/>cn.Execute "insert into point values( " &amp; dh + 1 &amp; ",'' ," &amp; zb(0) &amp; "," &amp; zb(1) &amp; ",'')" '点表<br/>End Sub<br/>Private Sub pline(point, tyl, obj2 As AcadEntity, cn, tyidh) '新建pl线处理过程<br/>Dim mx As New ADODB.Recordset<br/>&nbsp; Dim plzb As Variant '顶点坐标数组<br/>&nbsp; Dim ss, dh As Integer<br/>&nbsp; dh = 0<br/>&nbsp; Do While Not point.EOF<br/>&nbsp;&nbsp; 'dh = dh + 1<br/>&nbsp;&nbsp; dh = point.Fields("pointid")<br/>&nbsp;&nbsp; GoTo l<br/>&nbsp; Loop<br/>l:&nbsp; plzb = obj2.Coordinates<br/>&nbsp; ss = (UBound(plzb) + 1) / 2<br/>&nbsp; Dim zb As Variant<br/>&nbsp; For i = 0 To ss - 1<br/>&nbsp;&nbsp; zb = obj2.Coordinate(i) '单个点坐标<br/>&nbsp;&nbsp; mx.Open "select * from point where x=" &amp; zb(0) &amp; " and y=" &amp; zb(1) &amp; "", cn, adOpenDynamic, adLockBatchOptimistic<br/>&nbsp;&nbsp; If Not mx.EOF Then<br/>&nbsp;&nbsp;&nbsp; cn.Execute "insert into tylj values( " &amp; tyidh &amp; "," &amp; i + 1 &amp; "," &amp; mx.Fields("pointid") &amp; ")"<br/>&nbsp;&nbsp;&nbsp; Else<br/>&nbsp;&nbsp; cn.Execute "insert into tylj values( " &amp; tyidh &amp; "," &amp; i + 1 &amp; "," &amp; dh + 1 &amp; ")"<br/>&nbsp;&nbsp; cn.Execute "insert into point values( " &amp; dh + 1 &amp; ",'' ," &amp; zb(0) &amp; "," &amp; zb(1) &amp; ",'')"<br/>&nbsp;&nbsp; dh = dh + 1<br/>&nbsp;&nbsp; End If<br/>&nbsp;&nbsp; mx.Close<br/>&nbsp; Next i<br/>&nbsp; point.Close<br/>End Sub<br/>Private Sub li(poin, tyl, obj1 As AcadEntity, cn, tyidh) '新建line线处理<br/>Dim mx As New ADODB.Recordset<br/>Dim i, j, s As Integer<br/>i = 0<br/>Dim pt1 As Variant<br/>Dim pt2 As Variant<br/>pt1 = obj1.StartPoint<br/>pt2 = obj1.EndPoint<br/>Do While Not poin.EOF<br/>i = poin.Fields("pointid")<br/>&nbsp;'i = i + 1<br/>&nbsp;GoTo q<br/>&nbsp;'poin.MoveNext<br/>&nbsp;Loop '计算点数<br/>q: mx.Open "select * from point where x=" &amp; pt1(0) &amp; " and y=" &amp; pt1(1) &amp; "", cn, adOpenDynamic, adLockBatchOptimistic<br/>&nbsp;If Not mx.EOF Then<br/>&nbsp;&nbsp; cn.Execute "insert into tylj values( " &amp; tyidh &amp; ",1," &amp; mx.Fields("pointid") &amp; ")"<br/>&nbsp;Else<br/>&nbsp;cn.Execute "insert into point values( " &amp; i + 1 &amp; ",'' ," &amp; pt1(0) &amp; "," &amp; pt1(1) &amp; ",'')"<br/>&nbsp;cn.Execute "insert into tylj values( " &amp; tyidh &amp; ",1," &amp; i + 1 &amp; ")"<br/>&nbsp;End If<br/>&nbsp;mx.Close<br/>&nbsp;i = i + 1<br/>&nbsp;mx.Open "select * from point where x=" &amp; pt2(0) &amp; " and y=" &amp; pt2(1) &amp; "", cn, adOpenDynamic, adLockBatchOptimistic<br/>&nbsp;If Not mx.EOF Then<br/>&nbsp;&nbsp; cn.Execute "insert into tylj values(" &amp; tyidh &amp; ",2," &amp; mx.Fields("pointid") &amp; ")"<br/>&nbsp;&nbsp; Else<br/>&nbsp;&nbsp;&nbsp;&nbsp; cn.Execute "insert into point values( " &amp; i + 1 &amp; ",'' ," &amp; pt2(0) &amp; "," &amp; pt2(1) &amp; ",'')"<br/>&nbsp;&nbsp;&nbsp;&nbsp; cn.Execute "insert into tylj values( " &amp; tyidh &amp; ",2," &amp; i + 1 &amp; ")"<br/>&nbsp; End If<br/>&nbsp;poin.Close<br/>&nbsp;'tyl.Close</p><p>End Sub<br/>Private Sub kzsj(obj As AcadEntity, cn, dwname) '加扩展数据<br/>wj = "d:\b.txt"<br/>Open wj For Append As #1<br/>Dim sjk As New ADODB.Recordset<br/>Dim id As Long<br/>id = 0<br/>sjk.Open "select * from tysx order by cadid desc ", cn, adOpenDynamic, adLockReadOnly<br/>Do While Not sjk.EOF<br/>&nbsp; id = sjk.Fields("cadid")<br/>&nbsp; GoTo w<br/>&nbsp; 'sjk.MoveNext<br/>Loop<br/>w: sjk.Close<br/>For Each obj In ThisDrawing.ModelSpace<br/>Dim xtype As Variant<br/>Dim xdata As Variant<br/>obj.GetXData "", xtpye, xdata<br/>If VarType(xdata) = 0 Then '判断是否有扩展数据。<br/>Dim datatype(0 To 7) As Integer<br/>Dim data(0 To 7) As Variant<br/>datatype(0) = 1001: data(0) = "tete"<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) = id + 1<br/>datatype(5) = 1070: data(5) = 5656<br/>datatype(6) = 1071: data(6) = 32332<br/>datatype(7) = 1042: data(7) = 10<br/>obj.SetXData datatype, data<br/>ThisDrawing.Application.Update<br/>id = id + 1<br/>Write #1, data(4)<br/>End If<br/>Next obj<br/>Close #1<br/>End Sub<br/>Private Function dqkzsj(obj As AcadEntity) '读取扩展数据<br/>Dim id As Long<br/>Dim xtype As Variant<br/>Dim xdata As Variant<br/>obj.GetXData "", xtpye, xdata<br/>id = tyidh<br/>End Function</p><p>'图形中删除图元时库中也删除。<br/>Private Sub findid(dwname, cn)<br/>Dim js As Integer<br/>Dim id As Long<br/>Dim rst1 As New ADODB.Recordset<br/>rst1.Open "select * from tysx where dwid in (select dwid from tyname where dwname='" &amp; dwname &amp; "')", cn, adOpenDynamic, adLockBatchOptimistic</p><p>&nbsp;&nbsp; Do While Not rst1.EOF<br/>&nbsp;&nbsp;&nbsp;&nbsp; js = 0<br/>&nbsp;&nbsp;&nbsp;&nbsp; id = rst1.Fields("cadid")<br/>&nbsp;&nbsp;&nbsp;&nbsp; For Each obj In ThisDrawing.ModelSpace<br/>&nbsp;&nbsp;&nbsp;&nbsp; Dim xtype As Variant<br/>&nbsp;&nbsp;&nbsp;&nbsp; Dim xdata As Variant<br/>&nbsp;&nbsp;&nbsp;&nbsp; obj.GetXData "", xtpye, xdata<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If id = xdata(4) Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; js = js + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'cn.Execute "delete tytable where id= " &amp; obj.ObjectID &amp; ""<br/>&nbsp;&nbsp;&nbsp;&nbsp; Next obj<br/>&nbsp;&nbsp;&nbsp;&nbsp; If js = 0 Then<br/>&nbsp;&nbsp;&nbsp;&nbsp; cn.Execute "delete tysx where cadid=" &amp; rst1.Fields("cadid") &amp; ""<br/>&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp;&nbsp; rst1.MoveNext<br/>&nbsp;&nbsp; Loop<br/>rst1.Close<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp; <br/>End Sub</p><p></p>

tcsl9621 发表于 2007-6-15 23:24:00

好的,学习学习。

laoxie_198 发表于 2007-6-21 08:02:00

<p>不足的地方请指出</p>

chengdd 发表于 2007-6-27 22:32:00

<p>谢谢你分享!</p>

autocadvba 发表于 2007-7-17 17:54:00

<p>多谢您提供的宝贵代码!</p>

laoxie_198 发表于 2007-7-18 08:46:00

<p>大家互相学习吗?</p><p></p>

xiangyu1983 发表于 2007-8-22 20:13:00

<p>程序还简洁,但数据库表太多,也不利于以后读取数据库信息再作图,实现网络协同。用一个表写入所有平面图形几何信息那就好了。&nbsp; </p>

laoxie_198 发表于 2007-8-28 19:36:00

<p>写到同一个表里,数据太混乱了.读取信息在作图的模块我已经写好了,感觉在图形数据很大的时候运行速度比较吗,正在想办法改进.还请高手们多多执教</p>

xiangyu1983 发表于 2007-8-28 20:05:00

<p>我论文的一部分正是在做这东西 ,&nbsp; 我思路是把二维图形信息入库,建了三个表,一个用于作图时读取数据用的,一个总表,是对所进行操作和所操作图形信息查询用的,&nbsp; 还有一个就是删除图形的表(避免画出重复的图)。&nbsp;&nbsp; 我可能是机子配置所限,在一次录入大量复杂图形信息时比较慢(例如一次录入很多复杂多段线)。在读取大量信息画图是还算快。</p><p>&nbsp;&nbsp;&nbsp; 写在同一表不混乱,且避免重复建表的麻烦。使数据库维护和查询更方便。</p>

laoxie_198 发表于 2007-9-1 19:14:00

<p>谢谢楼上的朋友,我去试试将数据写在一个表里。不过感觉有点混乱了。</p>
页: [1] 2
查看完整版本: [原创]cad图形写入SQL数据库实例