これでUCSのZ軸が全てうちに向くか検証。
'/* Reason:UCS自動配置において、Z軸の向きを内側に向けるなど、座標系の方向を動的に修正・反転するためのiLogicコード */
Dim doc As PartDocument = ThisDoc.Document
Dim compDef As PartComponentDefinition = doc.ComponentDefinition
' 1. 修正したい対象のUCS(ユーザー座標系)の名前を指定して取得
Dim ucsName As String = "UCS_Port1" ' ←ここにお使いのUCS名を設定してください
Dim targetUcs As UserCoordinateSystem = Nothing
For Each ucs As UserCoordinateSystem In compDef.UserCoordinateSystems
If ucs.Name = ucsName Then
targetUcs = ucs
Exit For
End If
Next
' 対象のUCSが見つからない場合は処理を抜ける
If targetUcs Is Nothing Then
MessageBox.Show("指定されたUCS【" & ucsName & "】が見つかりません。", "iLogicエラー")
Return
End If
' 2. 方向転換の条件分岐(例:バルブのタイプや配置フラグによって向きを変える)
' ※実務のパラメータ名(例: Valve_Type や Direction_Flag)に書き換えてください
Dim invertZ As Boolean = False
If Parameter("Valve_Type") = "内側向き仕様" Then
invertZ = True
End If
' 3. 幾何関係を定義するためのオブジェクトを作成
Dim tg As TransientGeometry = ThisApplication.TransientGeometry
' 現在のUCSの配置位置(原点)をキープ
Dim originPoint As Point = targetUcs.OriginPlane.Geometry.Origin
' 4. Zの向きを内側にする(軸ベクトルの再定義)
' 標準のX軸、Y軸のベクトルを設定
Dim xAxisVector As Vector = tg.CreateVector(1, 0, 0)
Dim yAxisVector As Vector = tg.CreateVector(0, 1, 0)
If invertZ = True Then
' Zの向きを内側(反転)にしたい場合、X軸またはY軸を反転させてZ軸の向きを制御します
' 右手系の法則に従い、Y軸を反転させるとZ軸が逆(内側)を向きます
yAxisVector = tg.CreateVector(0, -1, 0)
End If
' 5. UCSの定義(ジオメトリ)を新しい向きで再適用
Try
Dim ucsDef As UserCoordinateSystemDefinition = compDef.UserCoordinateSystems.CreateDefinition()
ucsDef.SetOriginAndAxes(originPoint, xAxisVector, yAxisVector)
' 既存のUCSに新しい定義を上書きして向きを修正
targetUcs.Definition = ucsDef
' 画面を更新
ThisApplication.ActiveView.Update()
Catch ex As Exception
MessageBox.Show("UCSの向き修正中にエラーが発生しました: " & ex.Message, "システムエラー")
End Try

コメント