Public Class 文字整理
Shared Data As New List(Of String)
Shared NewData As New List(Of String)
Shared CurLg As Integer
Shared CurLy As Integer
Shared CurLc As Integer
Shared CurLt As Integer
Shared Cur書体 As String
Shared 文字大きさ As Integer
Shared CurSc(9) As Integer
Shared hcw(9) As Single
Shared hch(9) As Single
Shared hcd(9) As Single
Shared 任意文字 As New cls文字
Public Shared Sub Main()
前準備()
図形取得()
寸法.正規化()
文字.角度修正()
文字.記入()
寸法.記入()
ファイル書き込み()
End Sub
Private Shared Sub 前準備()
データ保存()
文字属性取得()
スケール取得()
End Sub
Private Shared Sub データ保存()
Dim FileName As IO.StreamReader
Dim mLine As String
Dim c As Integer
FileName = New IO.StreamReader("jwc_temp.txt", System.Text.Encoding.Default)
Do While FileName.Peek <> -1
c = 0
mLine = FileName.ReadLine
Data.Add(mLine)
Loop
FileName.Close()
End Sub
Private Shared Sub 図形取得()
Dim i As Integer
Dim c As Integer
For i = 0 To Data.Count - 1
c = 0
Dim mLine As String = Data(i)
Dim OneWord As String
OneWord = GetWord(c, " ", mLine)
Select Case Mid(Data(i), 1, 2)
Case "hq" : NewData.Add("hd")
Case "lg" : CurLg = Convert.ToInt32(Mid(Data(i), 3))
Case "ly" : CurLy = Convert.ToInt32(Mid(Data(i), 3))
Case "lc" : CurLc = Convert.ToInt32(Mid(Data(i), 3))
Case "lt" : CurLt = Convert.ToInt32(Mid(Data(i), 3))
Case "cn" : NewData.Add(mLine)
If Mid(OneWord, 3, 1) <> Chr(34) Then
文字大きさ = Convert.ToInt32(Mid(OneWord, 3), 16)
If 文字大きさ = 0 Then
任意文字属性(mLine)
End If
Else
Cur書体 = Mid(mLine, 4)
End If
End Select
If OneWord = "msg" Then
寸法図形取得(i)
Else
Select Case Mid(mLine, 1, 2)
Case "hq"
Case "ch", "cv", "cs", "cr", "co", "cp", "ct"
文字取得(mLine)
Case Else : NewData.Add(mLine)
End Select
End If
Next
End Sub
Private Shared Sub 任意文字属性(ByVal mLine As String)
Dim c As Integer
Dim dum As String
dum = GetWord(c, " ", mLine)
任意文字.hcw = GetWord(c, " ", mLine)
任意文字.hch = GetWord(c, " ", mLine)
任意文字.hcd = GetWord(c, " ", mLine)
任意文字.LC = Convert.ToInt32(GetWord(c, " ", mLine), 16)
End Sub
Private Shared Sub 文字取得(ByVal mLine As String)
Dim c As Integer
c = 0
Dim item As New cls文字
item.LG = CurLg
item.LY = CurLy
item.LC = CurLc
item.書体 = Cur書体
item.種別 = GetWord(c, " ", mLine)
item.基点x = GetWord(c, " ", mLine)
item.基点y = GetWord(c, " ", mLine)
Dim dx As Double = GetWord(c, " ", mLine)
Dim dy As Double = GetWord(c, " ", mLine)
If dx = 0 Then
If dy >= 0 Then
item.角度 = 90
Else
item.角度 = -90
End If
Else
item.角度 = Math.Atan2(dy, dx) * 180 / Math.PI
End If
item.文字 = Mid(GetWord(c, " ", mLine), 2)
item.大きさ = 文字大きさ
If item.大きさ = 0 Then
item.hcw = 任意文字.hcw
item.hch = 任意文字.hch
item.hcd = 任意文字.hcd
item.LC = 任意文字.LC
End If
文字.文字図形.Add(item)
End Sub
Private Shared Sub 寸法図形取得(ByRef i As Integer)
Dim c As Integer
Dim OneWord As String
Dim j As Integer
Dim item As New cls寸法図形
c = 0
j = i + 1
Do
c = 0
OneWord = GetWord(c, " ", Data(j))
Select Case Mid(OneWord, 1, 2)
Case "lt" : CurLt = Convert.ToInt32(Mid(Data(j), 3))
Case "lc" : CurLc = Convert.ToInt32(Mid(Data(j), 3))
Case Else : Exit Do
End Select
j = j + 1
Loop
i = j
item.LG = CurLg
item.LY = CurLy
item.LC = CurLc
item.LT = CurLt
c = 0
item.寸法線.始点x = GetWord(c, " ", Data(i))
item.寸法線.始点y = GetWord(c, " ", Data(i))
item.寸法線.終点x = GetWord(c, " ", Data(i))
item.寸法線.終点y = GetWord(c, " ", Data(i))
j = i + 1
Do
c = 0
OneWord = GetWord(c, " ", Data(j))
If Mid(OneWord, 1, 2) = "cn" Then
If Mid(OneWord, 3, 1) <> Chr(34) Then
文字大きさ = Convert.ToInt32(Mid(OneWord, 3), 16)
If 文字大きさ = 0 Then
任意文字属性(Data(j))
End If
Else
Cur書体 = Mid(Data(j), 4)
End If
End If
Select Case OneWord
Case "ch", "cv", "cs", "cr", "co", "cp", "ct" : Exit Do
End Select
NewData.Add(Data(j))
j = j + 1
Loop
i = j
item.寸法文字.LG = CurLg
item.寸法文字.LY = CurLy
item.寸法文字.書体 = Cur書体
item.寸法文字.種別 = OneWord
item.寸法文字.基点x = GetWord(c, " ", Data(i))
item.寸法文字.基点y = GetWord(c, " ", Data(i))
Dim dx As Double = GetWord(c, " ", Data(i))
Dim dy As Double = GetWord(c, " ", Data(i))
If dx = 0 Then
If dy >= 0 Then
item.寸法文字.角度 = 90
Else
item.寸法文字.角度 = -90
End If
Else
item.寸法文字.角度 = Math.Atan2(dy, dx) * 180 / Math.PI
End If
item.寸法文字.文字 = Mid(GetWord(c, " ", Data(i)), 2)
item.寸法文字.大きさ = 文字大きさ
If item.寸法文字.大きさ = 0 Then
item.寸法文字.hcw = 任意文字.hcw
item.寸法文字.hch = 任意文字.hch
item.寸法文字.hcd = 任意文字.hcd
item.寸法文字.LC = 任意文字.LC
End If
寸法.寸法図形.Add(item)
i = i + 1 '#まで
End Sub
Private Shared Sub 文字属性取得()
Dim i, j As Integer
Dim dum As String
Dim c As Integer
For i = 0 To Data.Count - 1
If Mid(Data(i), 1, 3) = "hcw" Then
c = 0
dum = GetWord(c, " ", Data(i))
For j = 0 To 9
dum = GetWord(c, " ", Data(i))
hcw(j) = dum
Next
End If
If Mid(Data(i), 1, 3) = "hch" Then
c = 0
dum = GetWord(c, " ", Data(i))
For j = 0 To 9
dum = GetWord(c, " ", Data(i))
hch(j) = dum
Next
End If
If Mid(Data(i), 1, 3) = "hcd" Then
c = 0
dum = GetWord(c, " ", Data(i))
For j = 0 To 9
dum = GetWord(c, " ", Data(i))
hcd(j) = dum
Next
End If
If Mid(Data(i), 1, 3) = "hcw" Then
c = 0
dum = GetWord(c, " ", Data(i))
For j = 0 To 9
dum = GetWord(c, " ", Data(i))
hcw(j) = dum
Next
End If
Next
End Sub
Private Shared Function スケール取得() As Double
Dim i, j As Integer
Dim dum As String
Dim c As Integer
For i = 0 To Data.Count - 1
c = 0
If Mid(Data(i), 1, 2) = "hs" Then
dum = GetWord(c, " ", Data(i))
For j = 0 To 9
dum = GetWord(c, " ", Data(i))
CurSc(j) = dum
Next
End If
Next
End Function
Private Shared Sub ファイル書き込み()
Dim i As Integer
Dim FileName As IO.StreamWriter
FileName = New IO.StreamWriter("jwc_temp.txt", False, System.Text.Encoding.Default)
For i = 0 To NewData.Count - 1
FileName.WriteLine(NewData(i))
Next
FileName.Close()
End Sub
Private Shared Function GetWord(ByRef C As Integer, ByVal Key As String, ByVal Str As String) As String
Dim i As Integer
Dim dum As String = ""
Dim Onest As String
C = C + 1
Do
Onest = Mid(Str, C, 1)
If Onest = "" Then Return ""
If Onest <> Key Then Exit Do
C = C + 1
Loop
For i = C To Len(Str)
If Mid(Str, i, 1) = Key Then
Exit For
Else
dum = dum & Mid(Str, i, 1)
End If
Next
C = i
Return dum
End Function
さらに続きます
Friend Class 寸法
Friend Shared 寸法図形 As New List(Of cls寸法図形)
Friend Shared Sub 正規化()
文字角度修正()
文字位置修正()
End Sub
Private Shared Sub 文字位置修正()
Dim i As Integer
Dim 大きさ As Integer
Dim Lg As Integer
For i = 0 To 寸法図形.Count - 1
Lg = 寸法図形(i).LG
大きさ = 寸法図形(i).寸法文字.大きさ
If 大きさ <> 0 Then
寸法図形(i).文字移動(hcw(大きさ - 1), hch(大きさ - 1), hcd(大きさ - 1), CurSc(Lg))
Else
寸法図形(i).文字移動(寸法図形(i).寸法文字.hcw, 寸法図形(i).寸法文字.hch, 寸法図形(i).寸法文字.hcd, CurSc(Lg))
End If
Next
End Sub
Private Shared Function is回転(ByVal Index As Integer) As Boolean
Dim dum As New cls寸法図形
dum = 寸法図形(Index)
If dum.寸法文字.角度 > 90 Then Return True
If dum.寸法文字.角度 <= -90 Then Return True
End Function
Private Shared Sub 文字角度修正()
Dim i As Integer
Dim 大きさ As Integer
Dim Lg As Integer
For i = 0 To 寸法図形.Count - 1
If is回転(i) = True Then
Lg = 寸法図形(i).LG
大きさ = 寸法図形(i).寸法文字.大きさ
If 大きさ <> 0 Then
寸法図形(i).寸法文字.文字回転(hcw(大きさ - 1), hch(大きさ - 1), hcd(大きさ - 1), CurSc(Lg))
Else
寸法図形(i).寸法文字.文字回転(寸法図形(i).寸法文字.hcw, 寸法図形(i).寸法文字.hch, 寸法図形(i).寸法文字.hcd, CurSc(Lg))
End If
End If
Next
End Sub
Friend Shared Sub 記入()
If 寸法図形.Count = 0 Then Exit Sub
Dim i As Integer
Dim dum As New cls寸法図形
Dim CN As String = ""
Dim 大きさ As Integer
dum.LG = 寸法図形(0).LG
dum.LY = 寸法図形(0).LY
dum.LT = 寸法図形(0).LT
dum.LC = 寸法図形(0).LC
CN = 寸法図形(0).寸法文字.CN
dum.寸法文字.書体 = 寸法図形(0).寸法文字.書体
NewData.Add("cn""" & dum.寸法文字.書体)
NewData.Add("lg" & Hex(dum.LG))
NewData.Add("ly" & Hex(dum.LY))
NewData.Add("lc" & Hex(dum.LC))
NewData.Add("lt" & Hex(dum.LT))
NewData.Add(CN)
For i = 0 To 寸法図形.Count - 1
If dum.LG <> 寸法図形(i).LG Then dum.LG = 寸法図形(i).LG : NewData.Add("lg" & Hex(dum.LG))
If dum.LY <> 寸法図形(i).LY Then dum.LY = 寸法図形(i).LY : NewData.Add("ly" & Hex(dum.LY))
If dum.寸法文字.書体 <> 寸法図形(i).寸法文字.書体 Then
dum.寸法文字.書体 = 寸法図形(i).寸法文字.書体
NewData.Add("cn""" & dum.寸法文字.書体)
End If
NewData.Add("msg")
If dum.LT <> 寸法図形(i).LT Then dum.LT = 寸法図形(i).LT : NewData.Add("lt" & Hex(dum.LT))
If dum.LC <> 寸法図形(i).LC Then dum.LC = 寸法図形(i).LC : NewData.Add("lc" & Hex(dum.LC))
NewData.Add(" " & _
寸法図形(i).寸法線.始点x & " " & 寸法図形(i).寸法線.始点y & " " & _
寸法図形(i).寸法線.終点x & " " & 寸法図形(i).寸法線.終点y)
If CN <> 寸法図形(i).寸法文字.CN Then
CN = 寸法図形(i).寸法文字.CN : NewData.Add(CN)
End If
大きさ = 寸法図形(i).寸法文字.大きさ
If 大きさ <> 0 Then
NewData.Add(寸法図形(i).寸法文字.JW(hcw(大きさ - 1), hch(大きさ - 1), hcd(大きさ - 1), CurSc(dum.LG)))
Else
NewData.Add(寸法図形(i).寸法文字.JW(CurSc(dum.LG)))
End If
NewData.Add("#")
Next
End Sub
End Class
さらに
Friend Class 文字
Friend Shared 文字図形 As New List(Of cls文字)
Private Shared Function is回転(ByVal Index As Integer) As Boolean
Dim dum As New cls文字
dum = 文字図形(Index)
If dum.角度 > 90 Then Return True
If dum.角度 <= -90 Then Return True
End Function
Friend Shared Sub 角度修正()
Dim i As Integer
Dim 大きさ As Integer
Dim Lg As Integer
For i = 0 To 文字図形.Count - 1
If is回転(i) = True Then
Lg = 文字図形(i).LG
大きさ = 文字図形(i).大きさ
If 大きさ <> 0 Then
文字図形(i).文字回転(hcw(大きさ - 1), hch(大きさ - 1), hcd(大きさ - 1), CurSc(Lg))
Else
文字図形(i).文字回転(文字図形(i).hcw, 文字図形(i).hch, 文字図形(i).hcd, CurSc(Lg))
End If
End If
Next
End Sub
Friend Shared Sub 記入()
If 文字図形.Count = 0 Then Exit Sub
Dim i As Integer
Dim dum As New cls文字
Dim CN As String = ""
Dim 大きさ As Integer
dum.書体 = 文字図形(0).書体
dum.LG = 文字図形(0).LG
dum.LY = 文字図形(0).LY
dum.LC = 文字図形(0).LC
CN = 文字図形(0).CN()
For i = 0 To 文字図形.Count - 1
If dum.書体 <> 文字図形(i).書体 Then dum.書体 = 文字図形(i).書体 : NewData.Add("cn""" & dum.書体)
If dum.LG <> 文字図形(i).LG Then dum.LG = 文字図形(i).LG : NewData.Add("lg" & Hex(dum.LG))
If dum.LY <> 文字図形(i).LY Then dum.LY = 文字図形(i).LY : NewData.Add("ly" & Hex(dum.LY))
If dum.LC <> 文字図形(i).LC Then dum.LC = 文字図形(i).LC : NewData.Add("lc" & Hex(dum.LC))
If CN <> 文字図形(i).CN Then CN = 文字図形(i).CN : NewData.Add(CN)
大きさ = 文字図形(i).大きさ
If 大きさ <> 0 Then
NewData.Add(文字図形(i).JW(hcw(大きさ - 1), hch(大きさ - 1), hcd(大きさ - 1), CurSc(dum.LG)))
Else
NewData.Add(文字図形(i).JW(CurSc(dum.LG)))
End If
Next
End Sub
End Class
End Class
Public Class cls寸法図形
Friend 寸法線 As New cls寸法線
Friend 寸法文字 As New cls文字
Public LG As Integer
Public LY As Integer
Public LC As Integer
Public LT As Integer
Private Sub 法線交点(ByRef X0 As Double, ByRef Y0 As Double)
If 寸法線.終点x - 寸法線.始点x = 0 Then '垂直
X0 = 寸法線.始点x
Y0 = 寸法文字.基点y
Else
'y=mx+n
'y=mfx+nf
Dim m As Double = (寸法線.終点y - 寸法線.始点y) / (寸法線.終点x - 寸法線.始点x)
'n=y-mx
Dim n As Double = 寸法線.始点y - m * 寸法線.始点x
If m = 0 Then '水平
X0 = 寸法文字.基点x
Y0 = 寸法線.始点y
Else
'm*mf=-1
Dim mf As Double = -1 / m
Dim nf As Double = 寸法文字.基点y - mf * 寸法文字.基点x
'mx0+n=mfx0+nf
'(m-mf)x0=-(n-nf)
X0 = -(n - nf) / (m - mf)
Y0 = m * X0 + n
End If
End If
End Sub
Private Function is平行() As Boolean
If Math.Abs(寸法線.角度 - 寸法文字.角度) < 0.001 Then Return True
If Math.Abs(寸法線.角度 - 寸法文字.角度 + 180) < 0.001 Then Return True
If Math.Abs(寸法線.角度 - 寸法文字.角度 - 180) < 0.001 Then Return True
If Math.Abs(寸法線.角度 - 寸法文字.角度 + 360) < 0.001 Then Return True
If Math.Abs(寸法線.角度 - 寸法文字.角度 - 360) < 0.001 Then Return True
Return False
End Function
Public Sub 文字移動(ByVal mW As Double, ByVal mH As Double, ByVal md As Double, ByVal Scale As Double)
If isTooLarge(mW, mH, md, Scale) = True Then Exit Sub
If Not is平行() Then Exit Sub
Dim 間隔 As Double
Dim X0 As Double
Dim Y0 As Double
法線交点(X0, Y0)
If 寸法線.角度 = 90 Or 寸法線.角度 = -90 Then
If 寸法文字.基点x > X0 Then
間隔 = 寸法文字.基点x - X0 - mH * Scale
Else
間隔 = X0 - 寸法文字.基点x
End If
寸法文字.基点x = X0 - 間隔
寸法文字.基点y = Y0
Else
If 寸法文字.基点y > Y0 Then
間隔 = ((寸法文字.基点x - X0) ^ 2 + (寸法文字.基点y - Y0) ^ 2) ^ 0.5
Else
間隔 = ((寸法文字.基点x - X0) ^ 2 + (寸法文字.基点y - Y0) ^ 2) ^ 0.5 - mH * Scale
End If
寸法文字.基点x = X0 - 間隔 * Math.Sin(寸法文字.角度 / 180 * Math.PI)
寸法文字.基点y = Y0 + 間隔 * Math.Cos(寸法文字.角度 / 180 * Math.PI)
End If
End Sub
Private Function isTooLarge(ByVal mW As Double, ByVal mH As Double, ByVal md As Double, ByVal Scale As Double) As Boolean
Dim 文字長 As Double = m_len(寸法文字.文字, mW, md, Scale)
Dim 寸法線長 As Double = ((寸法線.終点x - 寸法線.始点x) ^ 2 + (寸法線.始点y - 寸法線.終点y) ^ 2) ^ 0.5
If 文字長 > 寸法線長 Then Return True
End Function
Private Function m_len(ByVal str As String, ByVal mW As Double, ByVal md As Double, ByVal Scale As Double) As Double
Dim i As Integer
Dim m As Integer
' str:文字列 mw:jw全角文字幅 md:jw文字間隔 curScale:書き込みレイヤスケール
For i = 1 To Len(str)
m = System.Text.Encoding.GetEncoding("shift-jis").GetByteCount(Mid(str, i, 1))
If m = 1 Then '半角の場合
m_len = m_len + mW / 2 '半角文字の幅を加えて
If i <> Len(str) Then m_len = m_len + md / 2 '文字間隔の半分を加える
End If
If m = 2 Then '全角の場合
m_len = m_len + mW '全角文字の幅を加えて
If i <> Len(str) Then m_len = m_len + md '文字間隔を加える
End If
Next i
m_len = m_len * Scale
End Function
Public Class cls寸法線
Friend 始点x As Double
Friend 始点y As Double
Friend 終点x As Double
Friend 終点y As Double
Public ReadOnly Property 角度() As Double
Get
Dim dum As Double
If 終点x = 始点x Then
If 終点y >= 始点y Then
Return 90
Else
Return -90
End If
Else
dum = Math.Atan2(終点y - 始点y, 終点x - 始点x) * 180 / Math.PI
End If
Return dum
End Get
End Property
End Class
End Class