CAD二次开发数据分类汇总后生成表格

Imports System.Windows.Forms
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Colors
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
Imports Application = Autodesk.AutoCAD.ApplicationServices.Application
Imports Color = Autodesk.AutoCAD.Colors.Color

Imports lbForm = System.Windows.Forms



Public Class ModalFrom
    Dim obj As New lbForm.OpenFileDialog
    Dim sFile As String

    Dim AddDataSet As New DataSet() '实例化一个 DataSet数据集
    ReadOnly MD As New ModalDialo



    Private Sub Button21_Click(sender As Object, e As EventArgs) Handles Button21.Click
        Dim JD As Point3d = MD.JiaoDian
        NumericUpDown7.Text = Int(JD.X)
        NumericUpDown8.Text = Int(JD.Y)
    End Sub



    Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
        Dim sender1 As Object, e1 As EventArgs
        Button2_Click(sender1, e1)
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database

        '’统计模型空间各块参照出现的情况,并按数量进行降序排序, 名称,数量,缩略图 
        db.ChangeLayerColor("0", 2)
        Using trans As Transaction = db.TransactionManager.StartTransaction()
            Dim styleId As ObjectId = AddTableStyle("ColorTable")
            Dim table As Table = New Table()
            table.TableStyle = styleId
            table.Position = New Point3d(NumericUpDown7.Value, NumericUpDown8.Value, 0)
            table.SetSize(DataGridView1.Rows.Count - 1 + 2, 5)
            'table.SetSize(0 + 12, 4)
            table.SetRowHeight(3)
            table.SetColumnWidth(10)
            table.Cells(0, 0).TextString = "钢筋数量表"
            table.Cells(1, 0).TextString = "编号"
            table.Cells(1, 1).TextString = "直径" + vbCrLf + "(mm)"
            table.Cells(1, 2).TextString = "单根长" + vbCrLf + "(cm)"
            table.Cells(1, 3).TextString = "根数"
            table.Cells(1, 4).TextString = "总长" + vbCrLf + "(m)"

            Dim i As Integer = 2
            For j = 0 To DataGridView1.Rows.Count - 2

                table.Cells(i, 0).TextString = DataGridView1.Rows(j).Cells(0).Value.ToString() '’编号
                table.Cells(i, 1).TextString = DataGridView1.Rows(j).Cells(1).Value.ToString() '’直径
                table.Cells(i, 2).TextString = DataGridView1.Rows(j).Cells(2).Value.ToString() ''单根长
                table.Cells(i, 3).TextString = DataGridView1.Rows(j).Cells(3).Value.ToString() ''根数
                table.Cells(i, 4).TextString = Val(DataGridView1.Rows(j).Cells(2).Value.ToString()) * Val(DataGridView1.Rows(j).Cells(3).Value.ToString()) / 100 '’总长=单根长*根数

                i += 1
            Next


            Dim table2 As Table = New Table()
            table2.TableStyle = styleId
            table2.Position = New Point3d(NumericUpDown7.Value, NumericUpDown8.Value - 3 * (i + 2), 0)
            table2.SetSize(DataGridView2.Rows.Count + 2, 5)

            table2.SetRowHeight(3)
            table2.SetColumnWidth(10)

            table2.Cells(0, 0).TextString = "钢筋数量汇总"
            table2.Cells(1, 0).TextString = "直径" + vbCrLf + "(mm)"
            table2.Cells(1, 1).TextString = "总长" + vbCrLf + "(m)"
            table2.Cells(1, 2).TextString = "单位重" + vbCrLf + "(kg/m)"
            table2.Cells(1, 3).TextString = "总重" + vbCrLf + "(kg)"
            table2.Cells(1, 4).TextString = "合计" + vbCrLf + "(kg)"

            i = 2
            Dim cellRang As CellRange = CellRange.Create(table, 2, 4, DataGridView2.Rows.Count, 4)
            table2.MergeCells(cellRang)
            cellRang = CellRange.Create(table, DataGridView2.Rows.Count + 1, 0, DataGridView2.Rows.Count + 1, 3)
            table2.MergeCells(cellRang)
            For j = 0 To DataGridView2.Rows.Count - 2
                table2.Cells(i, 0).TextString = DataGridView2.Rows(j).Cells(0).Value.ToString() '’直径
                table2.Cells(i, 1).TextString = DataGridView2.Rows(j).Cells(1).Value.ToString() '’总长
                table2.Cells(i, 2).TextString = DataGridView2.Rows(j).Cells(2).Value.ToString() ''单位重
                table2.Cells(i, 3).TextString = DataGridView2.Rows(j).Cells(3).Value.ToString() ''总重
                i += 1
            Next
            table2.Cells(2, 4).TextString = Label3.Text
            table2.Cells(DataGridView2.Rows.Count + 1, 0).TextString = "C55混凝土(M3)"
            AddToModelSpace(db, table)
            AddToModelSpace(db, table2)

            '’字典汇总

            trans.Commit()
        End Using
    End Sub

    Public Shared Function AddTableStyle(ByVal style As String) As ObjectId
        Dim styleId As ObjectId
        Dim db As Database = HostApplicationServices.WorkingDatabase

        Using trans As Transaction = db.TransactionManager.StartTransaction()
            Dim dict As DBDictionary = CType(db.TableStyleDictionaryId.GetObject(OpenMode.ForRead), DBDictionary)

            If dict.Contains(style) Then
                styleId = dict.GetAt(style)
            Else
                Dim ts As TableStyle = New TableStyle()
                ts.SetBackgroundColor(Color.FromColorIndex(ColorMethod.ByAci, 8), CInt(RowType.TitleRow))
                ts.SetGridLineWeight(LineWeight.LineWeight030, CInt(GridLineType.OuterGridLines), CInt((RowType.DataRow Or RowType.HeaderRow Or RowType.TitleRow)))
                ts.SetGridLineWeight(LineWeight.LineWeight000, CInt(GridLineType.HorizontalBottom), CInt(RowType.HeaderRow))
                ts.SetGridLineWeight(LineWeight.LineWeight000, CInt(GridLineType.HorizontalTop), CInt(RowType.DataRow))
                ts.SetTextHeight(1, CInt((RowType.DataRow Or RowType.HeaderRow Or RowType.TitleRow)))
                ts.SetAlignment(CellAlignment.MiddleCenter, CInt((RowType.DataRow Or RowType.HeaderRow Or RowType.TitleRow)))
                dict.UpgradeOpen()
                styleId = dict.SetAt(style, ts)
                trans.AddNewlyCreatedDBObject(ts, True)
                trans.Commit()
            End If
        End Using

        Return styleId
    End Function

    Function AddToModelSpace(ByVal db As Database, ByVal ent As Entity) As ObjectId
        Dim entId As ObjectId ''用于返回添加到模型空间中的实体ObjectId
        ''定义一个指向当前数据库的事务处理,以添加实体
        Using trans As Transaction = db.TransactionManager.StartTransaction()
            ''以读方式打开块表
            Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
            ''以写方式打开模型空间块表记录.
            Dim btr As BlockTableRecord = trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
            entId = btr.AppendEntity(ent) ''将图形对象的信息添加到块表记录中
            trans.AddNewlyCreatedDBObject(ent, True) ''把对象添加到事务处理中
            trans.Commit() ''提交事务处理
        End Using

        Return entId ''返回实体的ObjectId
    End Function




    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
    Private Sub ImportData()
        Dim AddDataSet As New DataSet() '实例化一个 DataSet数据集
        If obj.FileName = "" Then
            With obj
                .ShowDialog()
                sFile = .FileName
            End With
        End If
        Dim AddRow As DataRow   '实例化一个DataRow数据行
        Dim RowValues(6) As Object  '定义一维数组
        AddDataSet.Tables.Add("batchs")  '向数据集中添加一个batchs表,表的名字是:batchs 

        '声明表头及该列的数据类型
        AddDataSet.Tables("batchs").Columns.Add("编号", Type.GetType("System.String"))
        AddDataSet.Tables("batchs").Columns.Add("直径(mm)", Type.GetType("System.String"))
        AddDataSet.Tables("batchs").Columns.Add("长度(cm)", Type.GetType("System.Double"))
        AddDataSet.Tables("batchs").Columns.Add("根数", Type.GetType("System.Int32"))
        AddDataSet.Tables("batchs").Columns.Add("单位重(kg/m)", Type.GetType("System.String"))
        AddDataSet.Tables("batchs").Columns.Add("总长(m)", Type.GetType("System.String"))
        AddDataSet.Tables("batchs").Columns.Add("总量(kg)", Type.GetType("System.String"))
        If Len(sFile) Then
            Dim sqltxt As String = My.Computer.FileSystem.ReadAllText(sFile).Trim
            '这样就逐条执行了
            '如果要单独把这些语句提出来
            Dim sqlcol() As String = Split(sqltxt, System.Environment.NewLine)
            For Each sql1 As String In sqlcol
                Dim tmp()
                tmp = sql1.Split(",")
                If tmp.Length <> 5 Then
                    MsgBox("导入数据不合格")
                    Exit Sub
                End If
                RowValues(0) = tmp(0)
                RowValues(1) = tmp(1)
                RowValues(2) = tmp(2)
                RowValues(3) = tmp(3)
                RowValues(4) = tmp(4)
                RowValues(5) = Val(tmp(2)) * Val(tmp(3)) / 100

                RowValues(6) = Val(tmp(2)) * Val(tmp(3)) * Val(tmp(4)) / 100

                AddRow = AddDataSet.Tables("batchs").NewRow
                AddRow.ItemArray = RowValues
                AddDataSet.Tables("batchs").Rows.Add(AddRow)
            Next
        End If
        DataGridView1.DataSource = AddDataSet.Tables.Item("batchs")
        For i = 0 To DataGridView1.Columns.Count - 1
            DataGridView1.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        Next


        Dim tb = AddDataSet.Tables.Item("batchs")
        ''遍历datagrideview1  5位置=2位置*3位置/100
        Try
            For Each r As DataRow In tb.Rows
                r("总长(m)") = Val(r("长度(cm)")) * Val(r("根数")) / 100
                r("总量(kg)") = Val(r("总长(m)")) * Val(r("单位重(kg/m)"))
            Next
        Catch ex As Exception
            MsgBox("数据不完善,请检查")
            Exit Sub
        End Try


        ''所有内容重新进文件
        ''
        ''重构一个新表
        Dim dtResult As System.Data.DataTable = New System.Data.DataTable()
        dtResult.Columns.Add("直径(mm)")
        dtResult.Columns.Add("总长(m)")
        dtResult.Columns.Add("单位重(kg/m)")
        dtResult.Columns.Add("总量(kg)")
        AddDataSet.Tables.Add(dtResult)
        DataGridView2.DataSource = AddDataSet.Tables(1)

        For i = 0 To DataGridView2.Columns.Count - 1
            DataGridView2.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        Next


        Dim zjlist As List(Of String) = New List(Of String) '直径分组
        For Each r As DataRow In tb.Rows
            Dim n = zjlist.IndexOf(r("直径(mm)"))
            If n = -1 Then
                zjlist.Add(r("直径(mm)"))
            End If
        Next
        zjlist.Sort()
        For Each zj As String In zjlist
            Dim dwz, zc, zl As Double
            zc = 0
            zl = 0
            dwz = 0
            For Each drItem As DataRow In tb.Rows

                If drItem("直径(mm)") = zj Then
                    zc += Val(drItem("总长(m)"))
                    zl += Val(drItem("总量(kg)"))
                    dwz = Val(drItem("单位重(kg/m)"))
                End If




            Next
            Dim drResult As DataRow = dtResult.NewRow()

            drResult("直径(mm)") = zj
            drResult("总长(m)") = zc
            drResult("总量(kg)") = zl
            drResult("单位重(kg/m)") = dwz
            dtResult.Rows.Add(drResult)
        Next

    End Sub





    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        obj.FileName = ""
        ImportData()
        Dim dwz As Double = 0
        For Each dgr As DataGridViewRow In DataGridView2.Rows
            If dgr.Cells("总量(kg)").Value Is Nothing Then
                Exit For
            End If
            dwz += dgr.Cells("总量(kg)").Value
        Next
        Label3.Text = dwz
    End Sub


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        If sFile = "" Then
            MsgBox("没有数据可以处理")
            Exit Sub
        End If

        ''将数据导回文件内

        ''Dim sr As New IO.StreamWriter(sFile & "Refresh")
        Dim sr As New IO.StreamWriter(sFile)

        For Each dgr As DataGridViewRow In DataGridView1.Rows
            If dgr.Cells("单位重(kg/m)").Value Is Nothing Then
                Exit For
            End If
            Dim s As String = ""
            s = dgr.Cells("编号").Value & ","
            s += dgr.Cells("直径(mm)").Value & ","
            s += dgr.Cells("长度(cm)").Value & ","
            s += dgr.Cells("根数").Value & ","
            s += dgr.Cells("单位重(kg/m)").Value
            sr.WriteLine(s)
        Next
        sr.Close()
        ImportData()

        Dim dwz As Double = 0
        For Each dgr As DataGridViewRow In DataGridView2.Rows
            If dgr.Cells("总量(kg)").Value Is Nothing Then
                Exit For
            End If
            dwz += dgr.Cells("总量(kg)").Value
        Next
        Label3.Text = dwz
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If DataGridView1.Rows.Count = 0 Then Exit Sub

        Dim list As List(Of String) = New List(Of String)()
        Dim list1 As List(Of String) = New List(Of String)()
        Dim rowIndex As Integer = DataGridView1.CurrentCell.RowIndex
        Dim row As Integer = DataGridView1.CurrentCell.RowIndex + 1
        Dim col As Integer = DataGridView1.CurrentCell.ColumnIndex + 1
        col = 1
        If 1 = row Then
            MessageBox.Show("已是第一行,无法上移")
            Return
        End If

        Dim count As Integer = DataGridView1.ColumnCount

        For i As Integer = 0 To count - 1
            list.Add(DataGridView1.Rows(row - 1).Cells(col - 1 + i).Value.ToString())
            list1.Add(DataGridView1.Rows(row - 2).Cells(col - 1 + i).Value.ToString())
        Next

        For j As Integer = 0 To count - 1
            DataGridView1.Rows(row - 1).Cells(col - 1 + j).Value = list1.ElementAt(j)
            DataGridView1.Rows(row - 2).Cells(col - 1 + j).Value = list.ElementAt(j)
        Next

        DataGridView1.Rows(rowIndex - 1).Cells(0).Selected = True
        DataGridView1.Rows(rowIndex).Cells(0).Selected = False
        DataGridView1.CurrentCell = DataGridView1.Rows(rowIndex - 1).Cells(0)
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        If DataGridView1.Rows.Count = 0 Then Exit Sub
        Dim list As List(Of String) = New List(Of String)()
        Dim list1 As List(Of String) = New List(Of String)()
        Dim rowIndex As Integer = DataGridView1.CurrentCell.RowIndex
        Dim row As Integer = DataGridView1.CurrentCell.RowIndex + 1
        Dim col As Integer = DataGridView1.CurrentCell.ColumnIndex + 1
        col = 1
        If DataGridView1.Rows.Count - 1 = row Then
            MessageBox.Show("已是最后一行,无法下移")
            Return
        End If

        Dim count As Integer = DataGridView1.ColumnCount

        For i As Integer = 0 To count - 1
            list.Add(DataGridView1.Rows(row - 1).Cells(col - 1 + i).Value.ToString())
            list1.Add(DataGridView1.Rows(row).Cells(col - 1 + i).Value.ToString())
        Next

        For j As Integer = 0 To count - 1
            DataGridView1.Rows(row - 1).Cells(col - 1 + j).Value = list1.ElementAt(j)
            DataGridView1.Rows(row).Cells(col - 1 + j).Value = list.ElementAt(j)
        Next

        DataGridView1.Rows(rowIndex).Cells(0).Selected = False
        DataGridView1.Rows(rowIndex + 1).Cells(0).Selected = True
        DataGridView1.CurrentCell = DataGridView1.Rows(rowIndex + 1).Cells(0)
    End Sub
End Class

猜你喜欢

转载自blog.csdn.net/laocooon/article/details/119989815