Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Assign Layer to part sketch in a drawing using iLogic

12 REPLIES 12
Reply
Message 1 of 13
They_Call_Me_Jake
1840 Views, 12 Replies

Assign Layer to part sketch in a drawing using iLogic

I am trying to assign a part sketch in a drawing to a specific layer but have been unable to find anything on how to do this. I have been able to turn the visibility of a part sketch on and off using a proxy. The problem is that you can't assign the sketch itself to a layer you have to select as edges first to assign the sketch to a specific layer. Is what I am trying to do possible? My thought was if there is anyway to, with iLogic, either select the sketch I want and then select its edges and assign it to a layer or create a drawing sketch based off of the part sketch and then assign the drawing sketch to a specific layer. Any help would be greatly appreciated.

12 REPLIES 12
Message 2 of 13

mrsmithTYTMZ,

 

I've got a start here, but am having a little trouble with one of my lines (which I've underlined).  I'm baffled as to why this won't work, so am hoping that someone smarter and wiser than I can shed some light on this.  Anyway, it at least tells me when the drawing sketch is named the same as a layer, so I feel like I am on the right track.  Hope this helps and have a most blessed day!

 

' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

' Set a reference to the active sheet.
Dim oActiveSheet As Sheet
oActiveSheet = oDrawDoc.ActiveSheet

' Create a Drawing View Object
Dim oDrawingView As DrawingView
Dim oDrawingSketch As DrawingSketch
Dim oSketchEntity As SketchEntity
Dim oLayer As Layer

For Each oDrawingView In oActiveSheet.DrawingViews
    For Each oDrawingSketch In oDrawingView.Sketches
        For Each oLayer In oDrawDoc.StylesManager.Layers
            If oDrawingSketch.Name = oLayer.Name Then
                MessageBox.Show(oDrawingSketch.Name & " sketch should be on the " & oLayer.Name & " layer", "Title")
                For Each oSketchEntity In oDrawingSketch.SketchEntities
                    oSketchEntity.Layer = oLayer
                Next
            End If

        Next
    Next
Next

iLogicVb.UpdateWhenDone = True

 

 

Peace,

Pete

Just a guy on a couch...

Please give a kudos if helpful and mark as a solution if somehow I got it right.
Message 3 of 13
Anonymous
in reply to: petestrycharske

@petestrycharske

 

 

Yes you are on a right track but since you where in drawing document you need to activate the sketch environment to change the layer of sketch. So I've added a simple code which was underlined that might help you most.

 

 

' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

' Set a reference to the active sheet.
Dim oActiveSheet As Sheet
oActiveSheet = oDrawDoc.ActiveSheet

' Create a Drawing View Object
Dim oDrawingView As DrawingView
Dim oDrawingSketch As DrawingSketch
Dim oSketchEntity As SketchEntity
Dim oLayer As Layer

For Each oDrawingView In oActiveSheet.DrawingViews
    For Each oDrawingSketch In oDrawingView.Sketches
        For Each oLayer In oDrawDoc.StylesManager.Layers
            If oDrawingSketch.Name = oLayer.Name Then
            MessageBox.Show(oDrawingSketch.Name & " sketch should be on the " & oLayer.Name & " layer", "Title")
            For Each oSketchEntity In oDrawingSketch.SketchEntities
                 oDrawingSketch.Edit
                 oSketchEntity.Layer = oLayer
                 oDrawingSketch.ExitEdit
            Next
            End If

        Next
     Next
Next

iLogicVb.UpdateWhenDone = True

 

 

If you find this was helpful for your query please accept it as solution / give a kudos Smiley Wink

 

Cheers,

SK.

Message 4 of 13
petestrycharske
in reply to: Anonymous

SK,

 

Brilliant!  That was definitely the missing piece to my puzzle.  I rewrote it slightly, so that the sketch wouldn't edit as often, but that is what I was missing.  Here is the revised code below with SK's missing pieces.  Hope this helps and have a most blessed day!

 

Peace,

Pete

 

' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

' Set a reference to the active sheet.
Dim oActiveSheet As Sheet
oActiveSheet = oDrawDoc.ActiveSheet

' Create a Drawing View Object
Dim oDrawingView As DrawingView
Dim oDrawingSketch As DrawingSketch
Dim oSketchEntity As SketchEntity
Dim oLayer As Layer

For Each oDrawingView In oActiveSheet.DrawingViews
    For Each oDrawingSketch In oDrawingView.Sketches
        For Each oLayer In oDrawDoc.StylesManager.Layers
            If oDrawingSketch.Name = oLayer.Name Then
                MessageBox.Show(oDrawingSketch.Name & " sketch should be on the " & oLayer.Name & " layer", "Title")
                oDrawingSketch.Edit
                For Each oSketchEntity In oDrawingSketch.SketchEntities
                    oSketchEntity.Layer = oLayer
                Next
                oDrawingSketch.ExitEdit

            End If

        Next
    Next
Next

iLogicVb.UpdateWhenDone = True

Just a guy on a couch...

Please give a kudos if helpful and mark as a solution if somehow I got it right.
Message 5 of 13

Thanks petestrycharske and srinivas.kasilingam for the quick replies. Question tho and correct me if I am wrong but won't this code only change a sketch that has been created in the drawing itself? What I am trying to accomplish is this....I have sketches that were created in the part or assembly itself and then after I have created the Base view from the part/Assembly I then right click on the part in the model tree in that view and select "Get Part Sketches" after which I select the part sketches I want to include. It is these said sketches that I am wanting to assign a layer to. I know that you can't even just click on the sketch in the model tree and then click a layer....you have to right click on the sketch in the model tree and then click "Select as Edges" before you can assign a layer to the entire sketch.
Message 6 of 13

Just as an example this is what I have to do to change the visibility of a model sketch....

 

Dim oDoc As DrawingDocument
	oDoc = ThisApplication.ActiveDocument

Dim oActiveSheet As Sheet
	oActiveSheet = oDoc.ActiveSheet

Dim oDrawingView As DrawingView
	oDrawingView = oActiveSheet.DrawingViews(3)
    
Dim oRefDoc As AssemblyDocument
	oRefDoc = oDrawingView.ReferencedDocumentDescriptor.ReferencedDocument
	
Dim oAssDef As AssemblyComponentDefinition
	oAssDef = oRefDoc.ComponentDefinition
    
Dim oOcc As ComponentOccurrence
	oOcc = oAssDef.Occurrences(1)
    
Dim oPart As PartDocument
	oPart = oOcc.Definition.Document

Dim oDef As PartComponentDefinition
	oDef = oPart.ComponentDefinition

Dim oSketch As PlanarSketch
	For Each oSketch In oDef.Sketches
		Dim oSketchProxy As PlanarSketchProxy
		oOcc.CreateGeometryProxy(oSketch, oSketchProxy)
			If oSketch.Name = "SK Back MTG Holes With Cutouts" Then
				oDrawingView.SetVisibility(oSketchProxy, True)
			End If
	Next

 

Message 7 of 13
Anonymous
in reply to: They_Call_Me_Jake

Hi @They_Call_Me_Jake

 

I have modified the code as per your requirement. Hope this will help you. 

 

' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

' Set a reference to the active sheet.
Dim oActiveSheet As Sheet
oActiveSheet = oDrawDoc.ActiveSheet

' Create a Drawing View Object
Dim oDrawingView As DrawingView
Dim oDrawingSketch As PlanarSketch
Dim oSketchEntity As SketchEntity
Dim oLayer As Layer
Dim oPartDoc As DocumentDescriptor
Dim oCd As ComponentDefinition
For Each oDrawingView In oActiveSheet.DrawingViews
    Dim oReferencedDocument As DocumentDescriptorsEnumerator
    oReferencedDocument = oActiveSheet.Parent.ReferencedDocumentDescriptors
        For Each oPartDoc In oReferencedDocument
            For Each oCd In oPartDoc.ReferencedDocument.ComponentDefinitions
                 For Each oDrawingSketch In oCd.Sketches
                    For Each oLayer In oDrawDoc.StylesManager.layers
                        If oDrawingSketch.name = oLayer.name Then
                            MsgBox oDrawingSketch.name & " sketch should be on the " & oLayer.name & " layer", "Title"
                            oDrawingSketch.Edit
                            For Each oSketchEntity In oDrawingSketch.SketchEntities
                                oSketchEntity.Layer = oLayer
                            Next
                            oDrawingSketch.ExitEdit
            
                        End If
                    Next
                Next
            Next
        Next
Next

iLogicVb.UpdateWhenDone = True

If you find this was helpful please accept this as solution /Press the kudos button Smiley Wink

 

Cheers,

SK.

Message 8 of 13
They_Call_Me_Jake
in reply to: Anonymous

@Anonymous Thank you for you help on this. We're close but I'm getting an error right after I close the message box:

 

Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

 

and this in the More Info tab:

 

System.Runtime.InteropServices.COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.SketchEntity.set_Layer(Layer )
at LmiRuleScript.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

it looks like the error is in the last For Loop.....I think...... If I comment out the line "oSketchEntity.Layer = oLayer" then the rule continues on and I get the message box that pops up about 15 times. 

 

I hope I'm not being to much of a pain but could I ask for a slight change? 99 percent of my drawing views have Assemblies in them and the Sketches that I want to assign to a layer are in a part in said assembly. Also I would like to specify the name of the sketch (If oSketch.Name = "Skatch Name" and the name of the layer (oLayer.Name = "Layer Name") as the sketches are not named the same as the layers.....is this possible?

 

Thanks again....you don't realize how much easier this will make my job. 🙂

 

 

Message 9 of 13

Can anyone explain why I would be getting this error when I use the code that @Anonymous posted. I have tried it on a few different parts and I get the same thing so I'm not sure what is wrong. Any help would be very much appreciated.

Message 10 of 13

I talked with someone that said for what I'm trying to do the code that @Anonymous posted would not work as it is for drawing sketches only and I am trying to change the layer of a sketch imported from a part. They told me to look at the ChangeLayer method however I get nothing we I search for this method. Does anyone have any insight on this and willing to help me out. Many thanks.....

Message 11 of 13

Just a bump to see if there is anyone that could help me with this problem. 

Message 12 of 13
Anonymous
in reply to: They_Call_Me_Jake

Maybe this helps, if still needed...


Dim oDoc As DrawingDocument
    oDoc = ThisApplication.ActiveDocument

Dim oActiveSheet As Sheet
    oActiveSheet = oDoc.ActiveSheet

Dim oDrawingView As DrawingView
    oDrawingView = oActiveSheet.DrawingViews(1) 'View of Sheet
    
Dim oRefDoc As AssemblyDocument
    oRefDoc = oDrawingView.ReferencedDocumentDescriptor.ReferencedDocument
    
Dim oAssDef As AssemblyComponentDefinition
    oAssDef = oRefDoc.ComponentDefinition
    
Dim oOcc As ComponentOccurrence
    oOcc = oAssDef.Occurrences(2) 'Assembly in sheet
    
Dim oPart As PartDocument
    oPart = oOcc.Definition.Document

Dim oDef As PartComponentDefinition
    oDef = oPart.ComponentDefinition

Dim oSketch As PlanarSketch
    For Each oSketch In oDef.Sketches
        Dim oSketchProxy As PlanarSketchProxy
            If oSketch.Name = "SK Back MTG Holes With Cutouts" Then
                oOcc.CreateGeometryProxy(oSketch, oSketchProxy)
                oDrawingView.SetVisibility(oSketchProxy, True)
######### Set Lines As DrawingCurves And Set Layer #############

         Dim oNewLayer As Layer
         oNewLayer = oDoc.StylesManager.Layers.Item("MyLayer Change Me")

          ' Get the TransientsObjects object to use later.
         Dim transObjs As TransientObjects
          transObjs = ThisApplication.TransientObjects

         Dim drawcurves As DrawingCurvesEnumerator
         drawcurves = oDrawingView.DrawingCurves(oSketchProxy)
         If Err.Number = 0 Then
            On Error GoTo 0
            ' Create an empty collection.
            Dim objColl As ObjectCollection
            objColl = transObjs.CreateObjectCollection()

            ' Add the curve segments to the collection.
            Dim drawCurve As DrawingCurve
            For Each drawCurve In drawcurves
               Dim segment As DrawingCurveSegment
               For Each segment In drawCurve.Segments
                  objColl.Add segment
               Next
            Next

            ' Change the layer of all of the segments.
            oDrawingView.Parent.ChangeLayer(objColl, oNewLayer)

         End If
         
##################         
         


            End If
    Next



If want it for more than a special view or part, I would recoment to change it to an "for each" code.

Message 13 of 13
Anonymous
in reply to: petestrycharske

Hi Pete,

 

Your code works really well and is what I am after (altering a sketch in a drawing document).

 

I was wondering if you where able to tweak it a little...?

 

Firstly I should give you a little bit more info about my drawing...

My .idw drawing has multiple sheets

On each sheet, within the first view, I have a sketch called "Workpiece size" which contains geometry on a layer called "Workpiece size"

 

tweaks...

1) I want to loop your code through all sheets within the drawing

2) I want to move all geometry within the sketch "Workpiece size" to a new layer

 

3) I want the new layer name to be set per sheet based on a Prompted Entry within each sheet.

For example...

 

On sheet 1 the Prompted Entry '{Item.Thickness}' = 18. I want to move the geometry within the sketch "Workpiece size" to a new layer called 'XLYP1H18'

 

On sheet 2 the Prompted Entry '{Item.Thickness}' = 13. I want to move the geometry within the sketch "Workpiece size" to a new layer called 'XLYP1H13' ('XLYP1H' will always be the same. the numer at the end will come from the 'Prompted Entry')

 

I have found some code that extracts the 'Prompted Entry' thanks to cadlinecommunity below.

 

Thanks in advance to any help you can give to either or all of the 3 questions

Regards

Mike

 

On Error Resume Next

Dim oDoc As DrawingDocument

oDoc = ThisApplication.ActiveDocument

Dim oSheet As Sheet

Dim oPromptEntry

 

Dim oCurrentSheet

oCurrentSheet = oDoc.ActiveSheet.Name

 

i = 1

For Each oSheet In oDoc.Sheets

'i = i+1

ThisApplication.ActiveDocument.Sheets.Item(i).Activate

       oTitleBlock=oSheet.TitleBlock

   oTextBoxes=oTitleBlock.Definition.Sketch.TextBoxes

   For Each oTextBox In oTitleBlock.Definition.Sketch.TextBoxes

   Select oTextBox.Text

       Case "{Item.Thickness}"

           oPromptEntry = oTitleBlock.GetResultText(oTextBox)

           iProperties.Value("Custom", "layer name")=oPromptEntry

 

   End Select

   Next

Next

 

 

******Your original code*********

 

' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

' Set a reference to the active sheet.
Dim oActiveSheet As Sheet
oActiveSheet = oDrawDoc.ActiveSheet

' Create a Drawing View Object
Dim oDrawingView As DrawingView
Dim oDrawingSketch As DrawingSketch
Dim oSketchEntity As SketchEntity
Dim oLayer As Layer

For Each oDrawingView In oActiveSheet.DrawingViews
    For Each oDrawingSketch In oDrawingView.Sketches
        For Each oLayer In oDrawDoc.StylesManager.Layers
            If oDrawingSketch.Name = oLayer.Name Then
                MessageBox.Show(oDrawingSketch.Name & " sketch should be on the " & oLayer.Name & " layer", "Title")
                oDrawingSketch.Edit
                For Each oSketchEntity In oDrawingSketch.SketchEntities
                    oSketchEntity.Layer = oLayer
                Next
                oDrawingSketch.ExitEdit

            End If

        Next
    Next
Next

iLogicVb.UpdateWhenDone = True

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report