VBA code for creating a special line style in Inventor drawing

VBA code for creating a special line style in Inventor drawing

m.joudivand
Enthusiast Enthusiast
566 Views
5 Replies
Message 1 of 6

VBA code for creating a special line style in Inventor drawing

m.joudivand
Enthusiast
Enthusiast

Dear Community

Are you able to let me know is it possible to create s special line style in Inventor drawing through running a VBA code. 

I have a code which is developed for a specific operation in the drawing environment, I want to merge it with the code which enables to define a line style automatically that will save a considerable amount of time for us.

My ultimate goal is to create an icon which opens a pop-up window that enables to define the line style and then rund the successive operations.

Waiting for your kind help.

Thanks in advance

Mohammad

0 Likes
Accepted solutions (2)
567 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor

Hi @m.joudivand.  I am not sure what you mean by "line style".  I do not think there is such a thing in Inventor.  The closest two things in Inventor I can think of are a Layer and a line type definition file ("*.lin" file extension).  The Layer object which exists exclusively in drawing documents, and can contain specifications for things like appearance/color, line type, line weight, and an option to scale by line weight.  Pretty much all drawing geometry that is created within a drawing automatically gets assigned to one of the many default layers for the type of geometry it is.  But you can generally edit/modify which geometries you want on which layers later.  The line type definition file is sort of like a complicated looking text file with data inside pertaining to the pattern(s) and spacing involved in certain line types, that is formatted very specifically.

 

If you are talking about the Edge Properties dialog you get when you select model geometry lines within a drawing view, then right-click and choose Properties..., we can not really access that through Inventor's API yet.  Plus the default values you will usually see displayed within that dialog are the default values of the Layer that the geometry belongs to.  If that is the dialog you were hoping to see pop-up within the second part of your code routine, the only thing I can think of would be to execute a command to make that dialog show, but I would not know how to capture the edits you make to the values in that dialog.  Or you could maybe try to create a similar looking dialog as a Windows Form using something like Visual Studio or VBA's UserForm interface, but implementing the specific controls within the dialog might be pretty challenging.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 6

m.joudivand
Enthusiast
Enthusiast

@WCrihfield  thanks for your explanations.

By line style I mean the line properties on a layer.

I just want to know is it possible to create a new layer by running a code. It would make my job easier because in that case I can add it to another code and then create its icon in the software.

 

0 Likes
Message 4 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @m.joudivand.  I think I may have something that you can work with then.  Below is the code for a VBA macro that can be used for ensuring a specifically named Layer is present in your drawing.  It first tries to find a layer by the specified name, and if found, makes sure it is a 'local' Layer, so it can be used within the document.  If it is not found, it is created, then all of its settings are set a certain way.  Right now, if it is found to already exist, it is not checking its settings, and just assuming that the settings are the way you want.  If this may not be the case, you can either restructure the code or add more code for ensuring the settings are the way you want them, even if the layer already exists.  This is an odd case, because there is no 'Add()' type method for creating new Layers...you just have to copy an existing Layer, then make any necessary adjustments to it.  You can also save this new local style back to the 'global' repository, so that it can be made available for other documents.  If you want to do that, there is a method of the Layer object called 'SaveToGlobal', that you can use.

You will need to edit the layer's name and its settings to suit your needs, language, and application.

Sub GetOrCreateLayer()
    If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
        Call MsgBox("A Drawing document must be active for this code to work. Exiting.", vbCritical, "")
        Exit Sub
    End If
    Dim oDDoc As DrawingDocument
    Set oDDoc = ThisDoc.Document
    Dim oDStMgr As DrawingStylesManager
    Set oDStMgr = oDDoc.StylesManager
    Dim oLayers As LayersEnumerator
    Set oLayers = oDStMgr.Layers
    Dim oTargetLayerName As String
    oTargetLayerName = "MyLayer"
    Dim oMyLayer As Layer
    Dim oLayer As Layer
    For Each oLayer In oLayers
        'If oLayer.InternalName = oTargetLayerName Then
        If oLayer.name = oTargetLayerName Then
            If oLayer.StyleLocation = StyleLocationEnum.kLibraryStyleLocation And _
                oLayer.StyleLocation <> StyleLocationEnum.kBothStyleLocation Then
                Set oMyLayer = oLayer.ConvertToLocal
            Else
                Set oMyLayer = oLayer
            End If
        End If
    Next
    If IsNothing(oMyLayer) Then
        Set oMyLayer = oLayers.Item(1).Copy(oTargetLayerName)
        'oMyLayer.AutomaticColor = True 'can ony be set to True, not False
        oMyLayer.Color = ThisApplication.TransientObjects.CreateColor(255, 0, 0)
        oMyLayer.Comments = "My layer comments"
        oMyLayer.LineType = LineTypeEnum.kContinuousLineType
        oMyLayer.LineWeight = 0.001
        oMyLayer.Plot = True
        oMyLayer.ScaleByLineWeight = False
        oMyLayer.Visible = True
        'oMyLayer.SaveToGlobal
    End If
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 6

m.joudivand
Enthusiast
Enthusiast

Dear @WCrihfield  thank you for the code, will try and let you know about the results.

It is much appreciated.

0 Likes
Message 6 of 6

m.joudivand
Enthusiast
Enthusiast
Accepted solution

Dear @WCrihfield 

First of all let me provide my sincere appreciation for your help.

I applied some modifications to the code to make it addapting my application.

The code is as follows.

Sub GetOrCreateLayer()
    If ThisDocument.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
        Call MsgBox("A Drawing document must be active for this code to work. Exiting.", vbCritical, "")
        Exit Sub
    End If
    Dim oDDoc As DrawingDocument
    Set oDDoc = ThisApplication.ActiveDocument
    Dim oDStMgr As DrawingStylesManager
    Set oDStMgr = oDDoc.StylesManager
    Dim oLayers As LayersEnumerator
    Set oLayers = oDStMgr.Layers
    Dim oTargetLayerName As String
    oTargetLayerName = "divandLayer"
    Dim oMyLayer As Layer
    Dim oLayer As Layer
    For Each oLayer In oLayers
        'If oLayer.InternalName = oTargetLayerName Then
        If oLayer.Name = oTargetLayerName Then
            If oLayer.StyleLocation = StyleLocationEnum.kLibraryStyleLocation And _
                oLayer.StyleLocation <> StyleLocationEnum.kBothStyleLocation Then
                Set oMyLayer = oLayer.ConvertToLocal
            Else
                Set oMyLayer = oLayer
            End If
        End If
    Next
    If oMyLayer Is Nothing Then
        Set oMyLayer = oLayers.Item(1).Copy(oTargetLayerName)
        'oMyLayer.AutomaticColor = True 'can ony be set to True, not False
        oMyLayer.Color = ThisApplication.TransientObjects.CreateColor(255, 0, 0)
        'oMyLayer.Comments = "Custumized Layer"
        oMyLayer.LineType = LineTypeEnum.kContinuousLineType
        oMyLayer.LineWeight = 0.001
        oMyLayer.Plot = True
        oMyLayer.ScaleByLineWeight = False
        oMyLayer.Visible = True
        'oMyLayer.SaveToGlobal
    End If
End Sub

 

To be mentioned, although "Comments" is a member of "Layer" class, but I couldn't apply it and that is why I have commented out line 31.

Sincerely yours

Mohammad

 

0 Likes