Wednesday, September 7, 2016

Getting what you want, not what you need....

We created some iLogic code to set the printer page size and orientation to 11x17, landscape with a scale of 0.5.  The code was ran to ensure that we could print out our station drawings half-size without going to the printer settings to set the page size, orientation and scale.  The code worked great, until we created a drawing template with an 11x17 sheet size as the default.

So, we added a custom property to the drawing called  "plot_scale" and then modified the code to look for that custom property.  If the custom property was not found, the code would query the user for the scale.

A trigger was set to run the rule after the drawing has been opened.  Additionally, the rule has been defined as an external rule, to allow the rule to more easily be implemented on other systems.

iLogic Code:
 ' Rule Name: dwg11x17print  
 ' Date : 9/7/16  
 ' Purpose:  
 ' iLogic rule to set the printer to 11x17  
 ' and check for the scale via a custom iproperty "plot_scale"  
 Sub Main()  
 Dim oDOc as Document  
 oDoc = ThisDoc.Document  
 If oDoc.DocumentType <> Inventor.DocumentTypeEnum.kDrawingDocumentObject Then  
      Exit Sub  
 End If  
 Dim custPropSet As PropertySet  
 custPropSet = oDoc.PropertySets.Item("Inventor User Defined Properties")  
 Dim oPrintMgr As DrawingPrintManager  
 oPrintMgr = oDoc.PrintManager  
 Dim dScale As Double  
 Dim lPrintScale As Boolean  
 ' Try to see if the custom iProperty exists...  
 Try  
      dScale = custPropSet.item("plot_scale").Value  
      ' MessageBox.Show("Scale custom property = "&CStr(dScale), "iLogic")  
 Catch  
      lPrintScale = InputRadioBox("Select scale for printing","Half scale","Full scale" , True, Title :="SCALE LIST")  
      If lPrintScale Then  
           dScale = 0.5  
      Else   
           dScale = 1.0  
      End If  
 End Try  
 With oPrintMgr  
      .Orientation = kLandscapeOrientation  
      .AllColorsAsBlack = False  
      .ColorMode = 13314 ' Print using a gray scale   
      .ScaleMode = 13827 ' kPrintCustomScale  
      .Scale = dScale  
      .PaperSize = PaperSizeEnum.kPaperSize11x17  
 End With  
 End Sub  

Wednesday, August 17, 2016

Autodesk Inventor - The case of the shifting text...

' http://www.danielhansen.net/2014/06/how-to-format-code-and-syntax.html

First post, of what may be many more, or it could simply be the last, only time will tell.

When using text containing characters with features which hung below the bottom, lower case "g" or lower case "q" or in this instance the forward slash "/". The text would shift up in the sketch.  The sketch was fully constrained and the single line text was bottom justified.

WHAT WE HAD....

WHAT WE WANTED....

THE SOLUTION... 

We posted a query on the Autodesk Inventor forums asking for assistance and within the hour we had a solution.  Set the text justification to baseline, not bottom justification.

Another solution would have been to simply have had the vertical alignment of the text set to center justification.  

The justification configuration was the last piece in the puzzle of the drawing template.  We are going to attempt to keep the number of sheets to a minimum, but we currently can have up to eight sheets in a single file.  

We are using multiple custom iProperties to populate the title blocks of the various sheets along with an iLogic form to assist in filling out the sheets with the proper client information.



Lastly, there is some iLogic code to push the proper sketch symbols for the client and state onto the eight sheets.  

'CODE STARTS HERE:
Option Explicit
Sub Main()

Dim sheetprop As String
Dim iNumSheets as Integer 

Dim oDoc as DrawingDocument 
oDoc = ThisApplication.ActiveDocument

Dim oSheet As sheet
Dim iSheetCount As Integer
Dim i As Integer
Dim j As Integer

' Number of sheets requested in the control panel form
iNumSheets = iProperties.Value("Custom", "SHEET_NUMBER")

' MsgBox("iNumSheets = " & iNumSheets, vbOKOnly,"Number of Sheets")

j = 1
For Each oSheet In odoc.sheets
 sheetprop = "SHEET_"&Trim(Str(j))
 oSheet.Name = iProperties.Value("Custom", sheetprop)
 If j > iNumSheets And j <=8 Then ' Set the sheet flag to false
  'MsgBox("Sheet Number  = " & j, vbOKOnly,"Number of Sheets")
  Parameter(sheetprop & "_CHK") = False
 Else
  If j > 1 Then 
   Parameter(sheetprop & "_CHK") = True
  End If
 End If
 j = j + 1
Next

' Open up the Control Panel
iLogicForm.Show("DRAWING CONTROL PANEL", FormMode.NonModal)
iLogicVb.UpdateWhenDone = True

' Setup the COA number
Dim strState As String = STATE_BORDER 'STATE_BORDER IS A PARAMETER...
Dim strCoaNum as String 

Select Case strState
.
.
.
.
 Case Else
 strCoaNum = "NONE"
End Select

'Push value into the custom iproperty so it can be dropped into symbol
iProperties.Value("Custom", "STATE_COA") = strCoaNum'STATE_COANUMBER
iProperties.Value("Custom", "STATE") = STATE_BORDER
'RuleParametersOutput()

Dim sLogo As String

Select Case CLIENT

 Case Else
  sLogo = "BLANK"
End Select
' MessageBox.Show ( CLIENT & " - " & sLogo,"Client Selected")
' Update the client logo
'MsgBox ("Logo selected: "& sLogo)
If sLogo <> "BLANK" Then Call logoChk(sLogo)

' Set the printer and page size
Dim oPrintMgr As DrawingPrintManager
oPrintMgr = oDoc.PrintManager

With oPrintMgr
 .AllColorsAsBlack = False
 .ColorMode = 13314 ' Print using a gray scale 
 .ScaleMode = 13827 ' kPrintCustomScale
 .PaperSize = PaperSizeEnum.kPaperSize11x17
End With

End Sub

Sub logoChk(sketchAdd As String)
' Code utilizes the last four characters of the symbol name
' to see if the symbol is a logo.  Therefore only use 'LOGO' 
' at the end of the symbol name.

' Insertion point for the current logo and title block (1/22/15)
Dim logopt As Point2D = ThisApplication.TransientGeometry.Createpoint2d(70.1675,7.62)
'logopt.x = 70.1675
'logopt.y = 7.62

Dim oDrawDoc as DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

Dim oSymDef As SketchedSymbolDefinition    
'MsgBox ("Logo Name = " & sketchAdd)
oSymDef = oDrawDoc.SketchedSymbolDefinitions.Item(sketchAdd)

' MsgBox("Symbol Definition : " & oSymDef.Name)

Dim oSym as SketchedSymbol
' Dim oSheet As oDrawDoc.Sheet

Dim bLogoFound As Boolean = False

Dim insPt2D As Point2D

For Each oSheet In oDrawdoc.sheets
 'MsgBox ("Sheet Number :"& oSheet.Name)
 For Each oSym in oSheet.SketchedSymbols
  If Right(oSym.Name,4) = "LOGO" Then
   ' now we check to see if the logo file matches the 
   ' client selected.
   ' need to have a flag to let the program know 
   ' that a logo was found and replaced or not
   ' MsgBox ("Sheet Number: "&"-"&sketchAdd)
   If oSym.Name  <> sketchAdd Then
    'MsgBox ("Sheet Number:"& oSheet.Name &" - " & oSym.Name &" Check Me.")
    ' Get the insertion point
    insPt2D = oSym.Position
    ' We delete the current definition...
    oSym.Delete
    '...and replace, with a rotation angle of 0 and a scale of 1
    oSym = oSheet.SketchedSymbols.Add(oSymDef,insPt2D,0,1,)
    'MsgBox ("Insertion Point : " & insPt2D.x & "-" & insPt2D.y)
    bLogoFound = True ' Set flag to True
   Else
    blogofound = True
   End If
  End If
 Next
 If bLogoFound = False Then  ' Currently no logo on the sheet
        ' we need to insert a logo into the
        ' proper location
  'MsgBox ("Logo to be added : " & sketchAdd)
  oSym = oSheet.SketchedSymbols.Add(oSymDef,logopt,0,1,)
  bLogoFound = False ' Reset flag
 End If
Next

' Housekeeping:
' Close any expanded nodes on the browswer
Call CloseTree

End Sub 

Sub CloseTree()
'-----start of ilogic-----
'sort components in the browser
'ThisApplication.CommandManager.ControlDefinitions.Item _
'("AssemblyBonusTools_AlphaSortComponentsCmd").Execute

'set a reference to the document
Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument

'Set a reference to the top node of the active browser
Dim oTopNode As BrowserNode
oTopNode = oDoc.BrowserPanes.ActivePane.TopNode

Dim oNode As BrowserNode

For Each oNode In oTopNode.BrowserNodes
 ' If the node is visible and expanded, collapse it.
 If oNode.Visible = True And oNode.Expanded = True Then
  oNode.Expanded = False
 End If
Next

'zoom all
'ThisApplication.ActiveView.Fit
End Sub