見出し画像

Inventor / iLogic : AutoCADの基本色をInventorの外観に登録するiLogicルール

設計の初期段階において、部品の取り合いをわかりやすくするために、パーツに何か色を付けたい時があります。しかし、Inventorに用意されている外観ライブラリからだと大変に選びにくいです。その時に、AutoCADの色のような馴染みのある色を色番号で選択できると便利だと思い作りました。

Sub Main
	
	' 基本色追加 Ver 1.1 By YOJI TANAKA At 20/12/14
	' Add basic color	
		
	Dim doc As Inventor.Document = ThisDoc.Document
	Dim invApp As Inventor.Application = ThisApplication

	' ドキュメントの外観コレクションの取得 Document Appearance Collection
   Dim docAssets As Assets
   Try
       docAssets = doc.Assets
   Catch ex As Exception	' Case the document is assembly
       Dim doc2 As AssemblyDocument = invApp.ActiveDocument
       docAssets = doc2.Assets
   End Try

	' 外観オブジェクト(Asset)の新規作成 Creating a new appearance object (Asset)
	Dim appearance As Inventor.Asset
	Dim tobjs As Inventor.TransientObjects
	Dim color As Inventor.ColorAssetValue
	Dim floatValue As Inventor.FloatAssetValue

	Dim indx As Integer
	Dim RR As Integer
	Dim GG As Integer
	Dim BB As Integer
	Dim ColorName As String = ""

	For indx = 1 To 7
		GetRGB(RR, GG, BB, indx, ColorName)
		Try
			appearance = docAssets.Add(AssetTypeEnum.kAssetTypeAppearance, "Generic", ColorName, ColorName)
		Catch ex As Exception
			MessageBox.Show("Already registered", "AddBasicColor")
	    	Exit Sub
	    End Try
		tobjs = invApp.TransientObjects
		color = appearance.Item("generic_diffuse")
		color.Value = tobjs.CreateColor(RR, GG, BB)
	Next
End Sub
Private Sub GetRGB(ByRef RR As Integer, ByRef GG As Integer, ByRef BB As Integer, ByRef ACADColor As Integer, ByRef ColorName As String)
	
	Dim r As Double
	Dim G As Double
	Dim B As Double
	ColorName = "ACAD_" + LTrim(Str(ACADColor))
	Select Case ACADColor
		Case 1 : r = 1 : G = 0 : B = 0 : ColorName = "1_RED"
		Case 2 : r = 1 : G = 1 : B = 0 : ColorName = "2_YELLOW"
		Case 3 : r = 0 : G = 1 : B = 0 : ColorName = "3_GREEN"
		Case 4 : r = 0 : G = 1 : B = 1 : ColorName = "4_CYAN"
		Case 5 : r = 0 : G = 0 : B = 1 : ColorName = "5_BLUE"
		Case 6 : r = 1 : G = 0 : B = 1 : ColorName = "6_MAGENTA"
		Case 7 : r = 1 : G = 1 : B = 1 : ColorName = "7_WHITE"
	End Select

	RR = r * 255
	GG = G * 255
	BB = B * 255

End Sub 

サブプログラム GetRGB は 外観の名前、RGB各色のバイト値を作成しています。AutoCADの色番号の1は赤、2は黄、と決まっているのでこのような命名としました。また、Inventorで外観を選択する時のUIが外観名の昇順で表示されるので、番号から始まっていると選びやすい、というのもあります。

このコードを適当な名前、例えば「AddAcadBasicColor.iLogicVb」として外部ルールフォルダに保存しておくと便利です。

画像1

使い方は、screencastの動画を参照ください。


役に立った!という記事にはぜひサポートお願いします。サポート頂けると大変に励みになります。