2018/7/29

iLogic應用 檢查並建立使用者參數

2019/1/13 更新
最近在更新過的 Inventor 2019 版上執行以功能會發生以下錯誤:

"UnitTypeEnum未宣告。可能因為它的保護層級而導致無法存取。"

本來以為是我的電腦設定造成,例如資料夾保護設定、病毒程式防護等等,
後來關掉一堆系統保護都沒用,
回過頭看 iLogic 提供的參數指令,
在 2019 版多提供有關公差跟讀取外部 xml 檔建立新參數的功能,
有可能是因為如此而在 Inventor 程式中拿掉有關 UnitTypeEnum 宣告所造成,
所以新的方法是使用"載入 XML":

2019 iLogic 載入參數(包括建立新參數)
至於 XML 的內容,可以使用"儲存 XML"寫出一個 xml 檔,
再使用文字編輯器去看,應該不難理解裡面的參數定義,
所以可以自行刪除、新增、編輯 xml 內容來控制執行 "載入 XML"要載入(含新建)參數。

以下適用 Inventor 2019R.2 之前版本。

在使用 Inventor 的過程中,
範本檔經常會"與時俱進"的進行"進化",
常見的就是配合需求增加使用者自定參數,
但是如果是早期建立的範本檔通常就會缺少這些後期所建立的使用者自訂參數,
可能造成某些"規則"或"表單"找不到參數而無法執行,
所以如果可以在一些使用到這些新參數的規則中加入一個偵測參數存在與否,
並且可以在找不到參數的情況下自動建立新參數就太好了。

再次請出 Google 大神,下搜尋關鍵字:
" inventor ilogic create parameter not exist "

果然在第一條就找到!
國外的高手實在是又多又讚!

狀況如下:

如果我們要使用三個在原本檔案中不存在的參數:
名稱(文字)、鍍膜厚度(數值)、輸出與否(真/假)三種不同型式的使用者自訂參數,
可以使用以下語法:

oMyParameter = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters
第一行是宣告啟用使用者自訂參數設訂物件: oMyParameter

PS.
有時候會跟 Inventor 某些功能相衝,會找不到成員而發生錯誤,
將 Inventor 關掉再重新開啟就可以,這應該算是 Inventor 系統的 Bug.

Try
Parameter("名稱") = Parameter("名稱")
測試參數"名稱"是否存在

Catch
oParameter=oMyParameter.AddByValue("名稱","基板",UnitTypeEnum.kTextUnits)
如果參數"名稱"不存在就建立一個文字型式(UniTypeEnum.kTextUnits)的使用者參數"名稱",並且指定其值為"基板"

End Try
結束 Try

完整的建立三種使用者參數的範例如下:

oMyParameter = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters
Try
 Parameter("名稱") = Parameter("名稱")
Catch
 oParameter=oMyParameter.AddByValue("名稱","基板",UnitTypeEnum.kTextUnits)
End Try
Try
 Parameter("鍍膜厚度") = Parameter("鍍膜厚度")
Catch
 oParameter=oMyParameter.AddByValue("鍍膜厚度",20,UnitTypeEnum.kMicronLengthUnits)
End Try
Try
 Parameter("輸出與否") = Parameter("輸出與否")
Catch
 oParameter=oMyParameter.AddByValue("輸出與否",True,UnitTypeEnum.kBooleanUnits)
End Try

其中 UnitTypeEnum 後面指定的參數型式(數值、文字或真假)跟單位可以參考如下表:

NameValueDescription
kAcreAreaUnits11301Acre Area.
kAmpElectricalCurrentUnits11327Amp electrical Current.
kBooleanUnits11347Boolean (Yes/No).
kBTUWorkUnits11320BTU Work.
kCalorieWorkUnits11319Calorie Work.
kCandelaLuminousIntensityUnits11342Candela luminous intensity.
kCelsiusTemperatureUnits11296Celsius Temperature.
kCentimeterLengthUnits11268Centimeter Length.
kCircularMilAreaUnits11326CircularMil Area.
kCompositeUnits11322Composite.
kCoulombElectricalChargeUnits11330Coulomb electrical Charge.
kCupVolumeUnits11306Cup Volume.
kDatabaseAngleUnits11277Database units for angle -- ALWAYS Radian.
kDatabaseLengthUnits11267Database units for length -- ALWAYS Centimeter.
kDatabaseMassUnits11282Database units for mass -- ALWAYS Kilogram.
kDatabaseTemperatureUnits11294Database units for temperature -- ALWAYS Kelvin.
kDatabaseTimeUnits11289Database units for time -- ALWAYS Second.
kDefaultDisplayAngleUnits11276Current default display units for Angle.
kDefaultDisplayLengthUnits11266Current default display units for length.
kDefaultDisplayMassUnits11281Current default display units for Mass.
kDefaultDisplayTemperatureUnits11293Current default display units for Temperature.
kDefaultDisplayTimeUnits11288Current default display units for Time.
kDegreeAngleUnits11279Degree Angle.
kDyneForceUnits11312Dyne Force.
kErgWorkUnits11318Erg Work.
kFahrenheitTemperatureUnits11297Fahrenheit Temperature.
kFaradElectricalCapacitanceUnits11331Farad electrical Capacitance.
kFeetPerSecondSpeedUnits11299FeetPerSecond Speed.
kFootLengthUnits11273Foot Length.
kGallonVolumeUnits11303Gallon Volume.
kGammaMagneticInductionUnits11337Gamma magnetic Induction.
kGaussMagneticInductionUnits11338Gauss magnetic Induction.
kGradAngleUnits11280Grad Angle.
kGramMassUnits11284Gram Mass.
kHenryElectricalInductanceUnits11339Henry electrical Inductance.
kHertzFrequencyUnits11341Hertz Frequency.
kHorsePowerPowerUnits11316HorsePower Power.
kHourTimeUnits11292Hour Time.
kInchLengthUnits11272Inch Length.
kJouleWorkUnits11317Joule Work.
kKelvinTemperatureUnits11295Kelvin Temperature.
kKilogramMassUnits11283Kilogram Mass.
kKSIPressureUnits11310KSI Pressure.
kLbForceUnits11313Lb Force.
kLbMassMassUnits11286LbMass Mass.
kLiterVolumeUnits11302Liter Volume.
kLumenLuminousFluxUnits11343Lumen luminous flux.
kLuxIlluminationUnits11344Lux illumination.
kMaxwellMagneticFluxUnits11335Maxwell magnetic Flux.
kMeterLengthUnits11270Meter Length.
kMetersPerSecondSpeedUnits11298MetersPerSecond Speed.
kmhoElectricalConductanceUnits11333mho electrical Conductance.
kMicronLengthUnits11271Micron Length.
kMileLengthUnits11275Mile Length.
kMilesPerHourSpeedUnits11300MilesPerHour Speed.
kMilLengthUnits11324Mil Length.
kMillimeterLengthUnits11269Millimeter Length.
kMinuteTimeUnits11291Minute Time.
kMoleSubstanceUnits11345Mole Substance or gram molecular weight.
kNauticalMileLengthUnits11323NauticalMile Length.
kNewtonForceUnits11311Newton Force.
kOerstedMagneticInductionUnits11340Oersted magnetic Induction.
kOhmElectricalResistanceUnits11329Ohm electrical Resistance.
kOunceForceUnits11314Ounce Force.
kOunceMassUnits11287Ounce Mass.
kOunceVolumeUnits11307Ounce Volume.
kPascalPressureUnits11308Pascal Pressure.
kPintVolumeUnits11305Pint Volume.
kPSIPressureUnits11309PSI Pressure.
kQuartVolumeUnits11304Quart Volume.
kRadianAngleUnits11278Radian Angle.
kRPMAngularVelocityUnits11321RPM AngularVelocity.
kSecondTimeUnits11290Second Time.
kSiemensElectricalConductanceUnits11332Siemens electrical Conductance.
kSlugMassUnits11285Slug Mass.
kSteradianAngleUnits11325Steradian Angle.
kTeslaMagneticInductionUnits11336Tesla magnetic Induction.
kTextUnits11346Text (String).
kUnitlessUnits11265No dimension associated with this value.
kVoltElectricalVoltageUnits11328Volt electrical Voltage.
kWattPowerUnits11315Watt Power.
kWeberMagneticFluxUnits11334Weber magnetic Flux.
kYardLengthUnits11274Yard Length.
以上表格參考自Autodesk Knowledge Network網站:UnitsTypeEnum Enumerator

沒有留言:

張貼留言