`
tyl
  • 浏览: 50424 次
  • 性别: Icon_minigender_2
  • 来自: 西安
文章分类
社区版块
存档分类
最新评论

VB操作cad文件

阅读更多

//比较乱,没有系统的整理。

Module Module1
    Public acadApp As Autodesk.AutoCAD.Interop.AcadApplication
    Public successTempFilePath = "c:\\GASignSuccess.log"
    Public isOpenTempFilePath = "c:\\isAutocadOpen.log"
    Public autocadObjectString = "AutoCAD.Application.17"

    Sub Main()
        Dim arguments() As String
        Dim I As Integer
        Dim flag As String
        Dim param As String

        '1###
        '2###dwgFilePath$$$context$$$isfirst
        arguments = Split(Command$, "###")
        For I = LBound(arguments) To UBound(arguments)
            Select Case I
                Case 0
                    flag = arguments(I)
                    System.Console.WriteLine("flag:" + flag)
                Case 1
                    param = arguments(I)
                    If (flag = "1") Then
                        quitAutoCad()
                    Else
                        If (flag = "2") Then
                            System.Console.WriteLine("param:" + param)
                            GASign(param)
                        End If
                    End If
            End Select
        Next
    End Sub


    Sub GASign(ByVal Args As String)
        Dim acadDoc As Autodesk.AutoCAD.Interop.AcadDocument
        Dim arguments() As String
        Dim dwgFilePath As String
        Dim I As Integer
        Dim user As String
        Dim time As String
        Dim isFirst As String

        arguments = Split(Args, "$$$")
        For I = LBound(arguments) To UBound(arguments)
            Select Case I
                Case 0
                    dwgFilePath = arguments(I)
                    System.Console.WriteLine("dwgFilePath:" + dwgFilePath)
                Case 1
                    user = arguments(I)
                    'context = context & vbCrLf & "kkkkk"
                    System.Console.WriteLine("user:" + user)
                Case 2
                    time = arguments(I)
                    System.Console.WriteLine("time:" + time)
                Case 3
                    isFirst = arguments(I)
                    System.Console.WriteLine("isFirst:" + isFirst)
            End Select
        Next

        WriteInfo(successTempFilePath, "")
        WriteInfo(isOpenTempFilePath, "")

        Try
            acadApp = GetObject(, autocadObjectString)
        Catch ex As Exception
            WriteInfo(successTempFilePath, "GetObject Exception:" + ex.Message)
            System.Console.WriteLine("GetObject Exception:" + ex.Message)
        End Try

        If (acadApp Is Nothing) Then
            Try
                acadApp = CreateObject(autocadObjectString)
            Catch ex As Exception
                WriteInfo(successTempFilePath, "CreateObject Exception:" + ex.Message)
                System.Console.WriteLine("CreateObject Exception:" + ex.Message)
            End Try
        Else
            If (isFirst = "true") Then
                WriteInfo(isOpenTempFilePath, "yes")
            End If
        End If

        If (acadApp Is Nothing) Then
            Return
        Else

            If (dwgFilePath.Length > 0) Then
                Try
                    If (IO.File.Exists(dwgFilePath)) Then
                        acadDoc = acadApp.Documents.Open(dwgFilePath)
                    Else
                        WriteInfo(successTempFilePath, "File Exception:文件不存在")
                        Return
                    End If
                Catch ex As Exception
                    WriteInfo(successTempFilePath, "Open Exception" + ex.Message)
                    System.Console.WriteLine("Open Exception:" + ex.Message)
                    ' MsgBox(Err.Description)
                End Try
            End If
        End If

        If (acadDoc Is Nothing) Then
            Return
        Else


            Dim Attrs As Object
            Dim myModelSpace As Autodesk.AutoCAD.Interop.Common.AcadObject
            Dim J As Integer
            Dim modelName As String
            Dim modelObjectName As String
            Dim attributeObj As Autodesk.AutoCAD.Interop.Common.AcadAttribute

            For Each myModelSpace In acadDoc.ModelSpace
                Try
                    modelObjectName = myModelSpace.ObjectName
                Catch ex As Exception
                    'System.Console.WriteLine("ObjectName Exception:" + ex.Message)
                End Try
                If modelObjectName = "AcDbBlockReference" Then
                    Try
                        modelName = myModelSpace.name
                    Catch ex As Exception
                        'System.Console.WriteLine("name Exception:" + ex.Message)
                    End Try
                    If modelName = "PC_TITLE_BLOCK" Then
                        Try
                            Attrs = myModelSpace.GetAttributes
                            For J = LBound(Attrs) To UBound(Attrs)
                                If (Attrs(J).TagString = "批准") Then
                                    System.Console.WriteLine("user context:" + user)
                                    Attrs(J).TextString = user
                                    WriteInfo(successTempFilePath, "ok")
                                Else
                                    If (Attrs(J).TagString = "批准日期") Then
                                        System.Console.WriteLine("time context:" + time)

                                        ' Attrs(J).FieldLength = 0.3
                                        Try
                                            'attributeObj = Attrs(J)
                                            'MsgBox("The FieldLength of the attribute is " & Attrs(J).ScaleFactor, vbInformation, "FieldLength 示例")

                                            ' Dim currFieldLength As Integer
                                            ' currFieldLength = Attrs(J).FieldLength
                                            'Attrs(J).FieldLength = currFieldLength + 1

                                            '有效的控制字的大小:ScaleFactor和Height
                                            ' Attrs(J).ScaleFactor = 0.3  指定文字的缩放比例。

                                            '通过可以控制字的高度,字变小变大
                                            ' Attrs(J).Height = 4  ' 属性的高度

                                            '通过.....
                                            ' Define the scale
                                            ' Dim basePoint(0 To 2) As Double
                                            'Dim scalefactor As Double
                                            'basePoint(0) = 2 : basePoint(1) = 2.25 : basePoint(2) = 0
                                            'scalefactor = 0.9
                                            ' Scale the polyline
                                            ' Attrs(J).ScaleEntity(basePoint, scalefactor)

 

                                            Dim currentThickness As Double
                                            currentThickness = Attrs(J).thickness
                                            MsgBox("The currentThickness of the attribute is " & Attrs(J).thickness, vbInformation, "currentThickness 示例")

                                            Attrs(J).thickness = currentThickness - 30


                                            Attrs(J).TextString = getTime(time)
                                            System.Console.WriteLine("time context2:" + getTime(time))
                                            Attrs(J).Visible = True

                                            Attrs(J).Update()

                                        Catch ex As Exception
                                            System.Console.WriteLine("FieldLength Exception:" + ex.Message)
                                        End Try

                                        WriteInfo(successTempFilePath, "ok")
                                    End If
                                End If
                            Next
                        Catch ex As Exception
                            ' System.Console.WriteLine("GetAttributes Exception:" + ex.Message)
                        End Try
                    End If
                End If
            Next
        End If

        Try
            acadDoc.Close()
        Catch ex As Exception
            System.Console.WriteLine("Close Exception:" + ex.Message)
        End Try
    End Sub

    Sub quitAutoCad()
        Try
            acadApp = GetObject(, autocadObjectString)
        Catch ex As Exception
            WriteInfo(successTempFilePath, "quitAutoCad:GetObject Exception:" + ex.Message)
            System.Console.WriteLine("quitAutoCad:GetObject Exception:" + ex.Message)
        End Try
        If (acadApp Is Nothing) Then
        Else
            Try
                If (IO.File.Exists(isOpenTempFilePath)) Then
                    Dim Line As String
                    For Each Line In IO.File.ReadAllLines(isOpenTempFilePath)
                        System.Console.WriteLine("Line:" + Line)
                        If (Line = "yes") Then
                            Return
                        End If
                    Next
                    '  DeleteFile(isOpenTempFilePath)
                End If
            Catch ex As Exception
                System.Console.WriteLine("File Exception:" + ex.Message)
            End Try
            Try
                acadApp.Quit()
            Catch ex As Exception
                System.Console.WriteLine("Quit Exception:" + ex.Message)
            End Try
        End If
    End Sub

    Function WriteInfo(ByVal filePath As String, ByVal context As String)
        Dim fileStream As IO.FileStream
        If (IO.File.Exists(filePath)) Then
            Try
                IO.File.WriteAllText(filePath, context)
                WriteInfo = True
            Catch ex As Exception
                WriteInfo = False
                System.Console.WriteLine("WriteAllText Exception:" + ex.Message)
            End Try
        Else
            Try
                fileStream = IO.File.Create(filePath)
                IO.File.WriteAllText(filePath, context)
                WriteInfo = True
                fileStream.Close()
            Catch ex As Exception
                WriteInfo = False
                System.Console.WriteLine("Create and WriteAllText Exception:" + ex.Message)
            End Try
        End If
    End Function

    Function DeleteFile(ByVal filePath As String)
        Try
            If (IO.File.Exists(filePath)) Then
                IO.File.Delete(filePath)
                DeleteFile = True
            Else
                DeleteFile = True
            End If
        Catch ex As Exception
            DeleteFile = False
            System.Console.WriteLine("Delete Exception:" + ex.Message)
        End Try
    End Function


    Function getTime(ByVal time As String)
        Dim arguments As String()
        Dim I As Integer
        Dim year = ""
        Dim month = ""
        Dim day = ""
        Dim timestr = ""

        Try
            arguments = Split(time, "-")
            For I = LBound(arguments) To UBound(arguments)
                Select Case i
                    Case 2
                        year = arguments(I)

                    Case 0
                        month = arguments(I)
                    Case 1
                        day = arguments(I)

                End Select
            Next
            timestr = year + month + day
            getTime = timestr
        Catch ex As Exception
            getTime = timestr
            System.Console.WriteLine("getTime Exception:" + ex.Message)
        End Try

    End Function
End Module

分享到:
评论

相关推荐

    VB读取CAD文件

    总之,VB读取CAD文件涉及的主要知识点包括:VB编程基础、第三方库的使用(如NetDxf)、AutoCAD .NET API的调用、图形数据结构的理解以及文件I/O操作。通过学习和实践这些知识,你将能够高效地在VB环境中处理CAD文件...

    VB-CAD.rar_VB CAD 控件_VB绘图控件_cad 控件_cad控件_seedwg

    "cad控件"和"seedwg"则是这个控件的其他别名或特定功能,可能涉及到读取、显示和操作DWG(AutoCAD的默认文件格式)的能力。 描述中提到,这个软件是完全用VB编写的,并且包含所有算法的源代码。这意味着开发者可以...

    vb开发cad实例

    当结合AutoCAD(Computer-Aided Design)这样的专业计算机辅助设计软件时,VB可以成为一个强大的工具,用于自动化和定制CAD操作。"vb开发cad实例"这个主题,就是关于如何利用VB与AutoCAD进行交互,编写脚本来提高...

    VB制作 VB自动获取CAD内点、线坐标

    6. **关闭和释放资源**:完成操作后,关闭CAD文件并释放占用的资源。 标签“vb CAD 坐标”进一步强调了这个话题的核心技术点:VB编程,与CAD的交互,以及坐标处理。在开发这样的工具时,开发者需要熟悉VB的基本语法...

    VB读取预览autoCAD(dwg)文件示例

    在本示例中,我们将探讨如何使用VB来读取和预览AutoCAD的DWG文件,并将其保存为位图(Bitmap)格式。 首先,要实现这个功能,我们需要一个能够处理DWG文件的库或组件。这通常包括像“Autodesk .NET SDK”这样的开发...

    基于VB的CAD二次开发_画圆程序

    在VB中,我们可以使用AutoCAD的Object ARX(应用程序开发)库,这是Autodesk提供的一个SDK(软件开发工具包),包含了大量的类和方法,使得开发者可以直接操作CAD对象,如点、线、面、实体等。在这个画圆程序中,...

    VB引入CAD(dxf)文件

    在本场景中,我们关注的是如何使用VB来引入并操作CAD(Computer-Aided Design)文件,特别是DXF(Drawing Exchange Format)格式的文件。 DXF是一种由Autodesk为AutoCAD设计的数据交换格式,它允许不同CAD软件之间...

    AutoCAD二次开发中VB连接CAD的实例

    例如,以下VB代码片段展示了如何打开一个AutoCAD文件: ```vb Dim acadApp As Object Set acadApp = CreateObject("AutoCAD.Application") acadApp.Visible = True Dim acadDoc As Object Set acadDoc = ...

    VB编写的完整的CAD绘图程序.zip

    4. **文件操作**:如何读取和保存CAD文件格式,比如DWG或DXF。 5. **错误处理**:如何编写健壮的代码,处理可能出现的异常和错误情况。 6. **算法应用**:可能涉及到几何计算、坐标转换等数学算法。 通过深入研究这...

    VB.zip_CAD API VB_VB编程 CAD_VB编程CAD_cad绘图程序_vb.NET cad绘图

    在VB.NET环境中,开发者可以利用CAD(计算机辅助设计)API来创建和操作CAD绘图程序。这个"VB.zip"压缩包包含了一个简单的VB编程示例,它演示了如何使用API技术进行CAD应用的开发。对于初学者来说,这是一个非常实用...

    VB启动CAD的程序(含代码)

    4. **Command执行**:学习如何在VB中模拟用户在AutoCAD中输入命令,执行CAD操作。 5. **错误处理**:理解如何在VB中设置错误处理机制,确保即使在CAD操作失败时也能正确处理。 6. **CAD清理工具的实现**:分析清理...

    vb.net创建CAD工具条

    在VB.NET项目中,可以将图标文件(如PNG或ICO)作为资源添加,并在创建按钮时引用。 10. **编译与安装**:完成代码编写后,编译成DLL动态链接库,然后在AutoCAD中使用`NETLOAD`命令加载此DLL。若要永久安装,可将其...

    浏览AutoCAD文件的VB程序例子,包含控件安装文件

    在VB中操作AutoCAD文件的过程一般包括以下步骤: 1. **控件集成**:首先,需要在VB项目中引用AutoCAD的控件或DLL文件,这通常通过添加引用或者手动注册控件完成。 2. **实例化对象**:创建控件的对象实例,这将允许...

    CAD坐标获取工具,VB控制CAD

    1. **建立连接**:在VB中,首先需要创建一个AutoCAD应用实例并打开一个现有的DWG文件,或者创建一个新的绘图。 2. **激活文档**:使用VB代码使AutoCAD文档获得焦点,确保后续操作能正确执行。 3. **查询对象**:通过...

    VB6.0 简单的CAD绘图程序

    在VB6.0的CAD程序中,文件管理包括读取和写入CAD文件。通常,开发者需要实现文件的打开、保存、另存为等功能,确保用户可以方便地保存和加载他们的设计。对于不同的CAD文件格式,可能需要使用特定的库或API来实现...

    cad2007与vb冲突补丁文件

    总的来说,处理VB和CAD 2007的冲突问题需要对操作系统、编程和软件安装有深入的理解。通过应用专门的补丁,用户可以解决这些冲突,提高软件的稳定性和兼容性,从而提升工作效率。在日常工作中,及时关注软件更新和...

    VB 操作CAD

    当我们谈论“VB操作CAD”时,实际上是指使用VB来编写程序,通过自动化的方式与AutoCAD进行交互,实现对CAD图纸的创建、读取和修改等功能。 一、VB与CAD的接口 VB与CAD的交互是通过Autodesk提供的AutoCAD .NET API...

    vb.net 连接Autocad2008

    通过实例1连接AutoCAD2008的文件,你可以看到如何把这些知识点应用到实际的代码中,学习如何构建一个完整的连接和控制Autocad的程序。记住,实践是掌握这些技能的关键,不断尝试和调试会让你更加熟悉这个过程。

    VB控制AUTOCAD的教程续集_autocad教程_autocad_VB_

    在AUTOCAD中,VBA可以访问和操作CAD对象,如图层、线型、实体等。 2. **建立方程曲线** 在AUTOCAD中,利用VBA建立方程曲线涉及到数学建模和几何图形的绘制。这通常需要理解曲线的数学表示,比如贝塞尔曲线、样条...

    123.rar_CAD 3D绘图_VB 图形_cad_vb cad代码_绘图 CAD

    6. **文件导入导出**:可能涉及到CAD文件格式的解析和生成,如DXF、DWG等,以便与其他CAD软件交换数据。 7. **程序架构**:了解如何组织VB代码,使其结构清晰,易于维护和扩展。 总的来说,这个压缩包为VB程序员...

Global site tag (gtag.js) - Google Analytics