AnsweredAssumed Answered

Certain dimensions in drawings for 100 configs

Question asked by Lars Ahlzen on Sep 11, 2018
Latest reply on Sep 11, 2018 by Josh Brady

Hello people


I have a problem where I have a hex bolt in 100 different sizes and I need to make 100 different drawings. I have a drawing template with all the views that I need. But when I want to insert dimensions I don't want them all to insert. I only want length, diameter, head height and head width. When using InsertModelAnnotations3 I get all of them and a lot of them are unnecessary. I also don't want to use addDimension or whatever the method is called because I want the script to be as independent as possible (I would like to use the script for say a wood screw in the future with minimal adjustments). I had an idea that I could make a sketch with construction lines and driven dimensions around the model (see linked picture) and then implement those in code somehow, though I have no idea how to do that. Use SelectByID2 to all the driven dimensions in the sketch and implement them in the drawing? Don't think that works however. The code I have so far is down below and right now I am only focusing to fix the dimensions so I haven't implemented code to loop the configurations and save them yet, that comes later. It only takes the active part, puts it in the drawing with a predefined template,  selects view where to implement dimensions and then implement those dimensions. It is a lot of comments in the code but that is just for me to learn since I am new to this API stuff.


' Option Explicit

' Setting parenthesis means you get a return value


Dim swApp As SldWorks.SldWorks 'Allows access to sw interface

Dim swModel As SldWorks.ModelDoc2 'Allows access to sw document/model (Part, assembly or drawing)

Dim swRefModel As SldWorks.ModelDoc2 'Allows access to sw document/model (Part, assembly or drawing)

Dim swDrawing As SldWorks.DrawingDoc 'Allows access to sw drawing

Dim swSheet As SldWorks.Sheet 'Allows access to sw drawing sheet

Dim swDrwTemplate As String ' Creates drawing template variable as a string

Dim swModelDocExt As ModelDocExtension ' Allows access to the document/model, second package of functions/members and Modeldoc2 has the first package

' Dim swSelMgr As SldWorks.SelectionMgr ' Allows you to get information about selected objects




Sub main()


' Setting up your part/assembly

Set swApp = Application.SldWorks ' sets access to this interface

Set swRefModel = swApp.ActiveDoc ' Sets refereced model to the part or assembly document



If swRefModel.GetType <> swDocASSEMBLY And swRefModel.GetType <> swDocASSEMBLY Then ' Checks that user has correct document open

    swApp.SendMsgToUser "Open Part or Assembly Document"

    Exit Sub

End If


' choosing drawing template which includes dimension options and drawing views

swDrwTemplate = "C:\ProgramData\SOLIDWORKS\SOLIDWORKS 2018\templates\BoltDepot-Drawing.drwdot" ' Specified drawig template for drawing


If swDrwTemplate = "" Then ' Checks that drawing template exist

    MsgBox "Template is not found"


End If


' Insert part/assembly in to the drawing

Set swModel = swApp.NewDocument(swDrwTemplate, swDwgPaperSizes_e.swDwgPaperBsize, 0, 0) ' sets access to new document with default template

Set swDrawing = swModel ' Sets drawing variable as the newly formed drawing document

Set swSheet = swDrawing.GetCurrentSheet ' Assigns value to sheet, value is to use the active sheet (sheet1)

swDrawing.InsertModelInPredefinedView swRefModel.GetPathName() ' Inserts the Part/Assembly to the drawing in the predefined drawing views from the drawing template


' Select and activate the view

Dim swView As SldWorks.View

Dim DrwView1 As Boolean

Set swModelDocExt = swModel.Extension ' Can just typ DrwView.swModel.Extension."whatever function/member" directly

DrwView1 = swModelDocExt.SelectByID2("Drawing View1", "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0) ' Sets view as an ID, can also be dimension, feature, etc, probably better to use specific funtions for those though

DrwView1 = swDrawing.ActivateView("Drawing View1")


' Setting up dimensions for part/assembly in drawing

swDrawing.InsertModelAnnotations3 swImportModelItemsFromEntireModel, _

swInsertDimensionsMarkedForDrawing, False, True, False, False  ' Currently all the dimensions for the part


Instead of using swImportModelItemsFromEntireModel maybe using swImportModelItemsFromSelectedFeature? But since a sketch isn't a feature I have problems with this too. Or maybe I am just doing everything wrong I truly don't know so all help would be appreciated.


I send some more picture to show what I mean.