明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1906|回复: 0

CorelDRAW二次开发源码

[复制链接]
发表于 2010-12-30 16:17:12 | 显示全部楼层 |阅读模式
本人写的用于从CorelDRAW获取图形数据的源码,因为VB使用指针很麻烦,所以就使用access数据库存储图形数据。请多指教。
Dim m_oAdoCnn As ADODB.Connection
Public Sub output()
    ActiveDocument.Unit = cdrMillimeter
    ActiveDocument.ReferencePoint = cdrBottomLeft
    Set m_oAdoCnn = New ADODB.Connection
    m_oAdoCnn.Mode = adModeShareExclusive
    m_oAdoCnn.Open "Provider=Microsoft.jet.OLEDB.4.0;Data Source=filename.mdb;User ID=;Password=;Jet OLEDB:Database Password=;"
    Dim lShapeCount As Long
    lShapeCount = get_coreldraw_shape(m_oAdoCnn, ActivePage.Shapes, True)
    m_oAdoCnn.Close
    Set m_oAdoCnn = Nothing
End Sub
Private Function get_coreldraw_shape(ByVal m_oAdoCnn As ADODB.Connection, ByVal vShapes As Variant, Optional ByVal isResetId As Boolean = False) As Long
    Static lShapeId As Long
    If isResetId Then lShapeId = 1
    ActiveDocument.Unit = cdrMillimeter
    Dim oShape As CorelDRAW.Shape
    Dim oColorShape As CorelDRAW.Color
    Dim lEntityColor As Long
    Dim sObjectName As String
   
    For Each oShape In vShapes
        Select Case oShape.Type
            Case cdrRectangleShape, cdrEllipseShape, cdrPolygonShape, cdrCurveShape, cdrTextShape
                If oShape.Type <> cdrCurveShape Then
                    oShape.ConvertToCurves
                End If
                If oShape.Type <> cdrCurveShape Then
                    get_coreldraw_shape m_oAdoCnn, oShape.Shapes
                Else
                    If oShape.Outline.Type = cdrOutline Then
                        Set oColorShape = oShape.Outline.Color
                        oColorShape.ConvertToRGB
                        lEntityColor = RGB(oColorShape.RGBRed, oColorShape.RGBGreen, oColorShape.RGBBlue)
                    Else
                        lEntityColor = RGB(255, 255, 255)
                    End If
                    
                    Dim fX As Double, fY As Double, fWidth As Double, fHeight As Double
                    oShape.GetBoundingBox fX, fY, fWidth, fHeight
                    m_oAdoCnn.Execute "insert into Shapes (fShapeId,fShapeType,fShapeLength,fShapeColor,fBoundingBoxLeft,fBoundingBoxBottom,fBoundingBoxRight,fBoundingBoxTop) values (" & lShapeId & "," & cdrCurveShape & "," & oShape.Curve.Length & "," & lEntityColor & "," & fX & "," & fY & "," & fX + fWidth & "," & fY + fHeight & ")"
                    get_curve m_oAdoCnn, oShape.Curve, lShapeId, lEntityColor
   
                    lShapeId = lShapeId + 1
                End If
            Case cdrGroupShape
                get_coreldraw_shape m_oAdoCnn, oShape.Shapes
            Case Else
                If MsgBox("", vbYesNo, "") = vbNo Then
                    get_coreldraw_shape = 0
                    Exit Function
                End If
        End Select
    Next
    get_coreldraw_shape = lShapeId
End Function
Private Sub get_curve(ByVal m_oAdoCnn As ADODB.Connection, ByVal oCurve As CorelDRAW.Curve, ByVal lShapeId As Long, ByVal lEntityColor As Long)
    Dim isCurve As Integer
    Dim oSubPath As CorelDRAW.SubPath
    Dim oNode As CorelDRAW.Node
    Dim fLength As Double
    Dim fX As Double, fY As Double
   
    For Each oSubPath In oCurve.Subpaths
        oSubPath.GetPosition fX, fY
        If oSubPath.Closed Then
            m_oAdoCnn.Execute "insert into Subpaths (fShapeId, fSubpathId,fShapeColor,fSubpathStartPositionX,fSubpathStartPositionY,fSubpathEndPositionX,fSubpathEndPositionY,fStartToLastLength,fEndToLastLength) values (" & lShapeId & "," & oSubPath.Index & "," & lEntityColor & "," & oSubPath.StartNode.PositionX & "," & oSubPath.StartNode.PositionY & "," & oSubPath.StartNode.PositionX & "," & oSubPath.StartNode.PositionY & "," & fX & "," & fY & ")"
        Else
            m_oAdoCnn.Execute "insert into Subpaths (fShapeId, fSubpathId,fShapeColor,fSubpathStartPositionX,fSubpathStartPositionY,fSubpathEndPositionX,fSubpathEndPositionY,fStartToLastLength,fEndToLastLength) values (" & lShapeId & "," & oSubPath.Index & "," & lEntityColor & "," & oSubPath.StartNode.PositionX & "," & oSubPath.StartNode.PositionY & "," & oSubPath.EndNode.PositionX & "," & oSubPath.EndNode.PositionY & "," & fX & "," & fY & ")"
        End If
        
        For Each oNode In oSubPath.Nodes
            isCurve = False
            fLength = 0#
            If Not oNode.Segment Is Nothing Then
                fLength = oNode.Segment.Length
                If oNode.Index = 1 Then
                    If oNode.Segment.Type = cdrCurveSegment Then
                        m_oAdoCnn.Execute "insert into CurveSegments (fShapeId, fSubpathId, fNodeId, fSegmentLength, fNodePositionX, fNodePositionY, fStartControlPointX, fStartControlPointY, fEndControlPointX, fEndControlPointY) values (" & lShapeId & "," & oSubPath.Index & "," & oSubPath.Nodes.Count + 1 & "," & fLength & "," & oNode.PositionX & "," & oNode.PositionY & "," & oNode.Segment.StartingControlPointX & "," & oNode.Segment.StartingControlPointY & "," & oNode.Segment.EndingControlPointX & "," & oNode.Segment.EndingControlPointY & ")"
                    ElseIf oNode.Segment.Type = cdrLineSegment Then
                        m_oAdoCnn.Execute "insert into Nodes (fShapeId, fSubpathId, fNodeId, fSegmentLength, fNodePositionX, fNodePositionY) values (" & lShapeId & "," & oSubPath.Index & "," & oSubPath.Nodes.Count + 1 & "," & fLength & "," & oNode.PositionX & "," & oNode.PositionY & ")"
                    End If
                    fLength = 0#
                Else
                    If oNode.Segment.Type = cdrCurveSegment Then
                        isCurve = True
                        m_oAdoCnn.Execute "insert into CurveSegments (fShapeId, fSubpathId, fNodeId, fSegmentLength, fNodePositionX, fNodePositionY, fStartControlPointX, fStartControlPointY, fEndControlPointX, fEndControlPointY) values (" & lShapeId & "," & oSubPath.Index & "," & oNode.Index & "," & fLength & "," & oNode.PositionX & "," & oNode.PositionY & "," & oNode.Segment.StartingControlPointX & "," & oNode.Segment.StartingControlPointY & "," & oNode.Segment.EndingControlPointX & "," & oNode.Segment.EndingControlPointY & ")"
                    End If
                End If
            End If
            If isCurve = False Then
                m_oAdoCnn.Execute "insert into Nodes (fShapeId, fSubpathId, fNodeId, fSegmentLength, fNodePositionX, fNodePositionY) values (" & lShapeId & "," & oSubPath.Index & "," & oNode.Index & "," & fLength & "," & oNode.PositionX & "," & oNode.PositionY & ")"
            End If
        Next oNode
    Next oSubPath
End Sub

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 14:34 , Processed in 0.175648 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表