Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations IDS on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

SW API Question: bug when drawing lines, etc w/ macros 2

Status
Not open for further replies.

SWVB101

Mechanical
Jun 25, 2003
85
I have a question about SolidWorks API, dealing with using VB & Macros to draw lines in model sketches and drawing views...

If you have the display at "Zoom All" and you use a program to start drawing lines that are close together, the lines seem like they are using the sketch relation tools and 'snapping' together...

For example, if you are using a D Size drawing and make to equal parallel lines, side by side, .05 apart... the second will be drawn on top of the first (w/ coincident points) in stead of actually being .05 apart...
BUT if you Zoom In, then draw them, they are drawn correctly...

I am making a log-log grid using a program to automate the drawing of aprox. 130 vertical lines and 100 horizontal lines...

the lines are draw using...

'set up the extents...
X1 = Log(1)
Y1 = Log(500)
X2 = Log(150)
Y2 = Log(15000)

'set up the max extents in log() format... with origin at 0,0
maxX = X2 - X1
maxY = Y2 - Y1

'vertical line
Dline Log(X) - X1, 0, Log(X) - X1, maxY

'horizontal line
Dline 0, Log(Y) - Y1, maxY, Log(Y) - Y1

Sub DLine(a1 As Double, b1 As Double, a2 As Double, b2 As Double)
'Create a line on the active Sketch/Drawing View
'Part.CreateLineVB X1, Y1, Z1, X2, Y2, Z2

Part.CreateLineVB CM(a1), CM(b1), 0, CM(a2), CM(b2), 0
End Sub

'Convert to Metric (inches to meters for SW)
Function CM(X As Double) As Double
CM = X * 0.0254
End Function


*Note: in my program I use Option Explicit, and all coord values are Double, counters are Integer or Long... And I use multiple For:Next loops to draw the Grid at different intervals when the Grid lines start to get too close togeter...

Anyways, Like I said above...
The problem is that when the end points get to close together, using the viewable distance, they create coincident relationships causing multiple lines to be drawn on top of each other, rather than the way they are suposed to be draw...

The only fix I have found is to Zoom in up close and then draw the lines and then zoom back out to ALL...
Part.ViewZoomTo2 -0.0003, 0.0002, 2.82573E-16, 0.0006, -0.0003, 3.28574E-16
...
'Draw Lines
...
Part.ViewZoomtofit2


Now I am assuming it is doing this because automatic relationships are turned on...
Normally I want this to be turned on when I am manually sketching...
Is there a way to Programatically disable the auto-relationships while drawing the lines, in place of the zoom comands, the restore the settings when done drawing?

-OR-

Is there a different way (API command) to draw the lines that do not use the system settings (like Auto Relationships) to draw the lines, and actually draw them to the EXACT X,Y coords you specify???

I know this might be a little confusing, It is kind of hard to explain with words...

Any help/suggestions will be appriciated...
If you need me to give further explaination, please let me know...
 
Replies continue below

Recommended for you

I had a similar problem. I was toying with a program I downloaded from nhcad.com that read points from a text file and placed them in a 3D sketch.

One thing that helped was turning off auto-relations. I did this from inside the code, and had a marker so that the program would return that setting to what it was before.

Look up SetUserPreferenceToggle in the API SolidWorks help.

[bat]I may make you feel, but I can't make you think.[bat]
 
OK... So it's:

'toggle off
swApp.SetUserPreferenceToggle swSketchAutomaticRelations, False
...
'sketch lines
...
'toggle back on
swApp.SetUserPreferenceToggle swSketchAutomaticRelations, True


Thanks Alot,
I thought I had seen it somewhere before...
Have a start for the quick response...

Thanks,
--Josh--
 
hmmm...
Well, that worked for the relationships... BUT...

The problem seems to be caused by those dashed horizontal/vertical lines that extend from the endpoint of lines, ect... when your cursor is in-line with them...
And cause the End/Start point to snap to be in line with the point, horizontally or vertically...

I can't find anywhere in the options to turn this off...
Is it possible to turn it off?
 
Workaround idea:

When I sketch, I usually draww line deliberately skewed to avoid horizontal & vertical snaps and constraints.

Maybe this could work in your program. First draw the lines skewed and then move the endpoints.

[bat]I may make you feel, but I can't make you think.[bat]
 
I found it!

Tools>Sketch Settings...>Automatic Inferring Lines

I recorded a macro, and the vb command seems to be...

Part.AutoInferToggle

***Note: this option is Sketch Specific...

It must be turned on/off on each individual sketch...

And is NOT a system option...

 
SWVB101

Is that each sketch or each part?

Regg
 
each sketch...

within a part, it is possible for 1 sketch to have the option enabled and another to have it disabled...

-Josh
 
Actually, a simple call to ModelDoc2::SetAddToDB = True will solve your problem. Any entities you add will be added directly to the database, skipping any tendencies SW has to snap the objects. When you change your SW options and settings, you are modifying them for the user interface as well. If you use the features while modeling in SW, you should leave them on. This call will eliminate the need to toggle all of your settings.
Code:
'(Assuming Part is a ModelDoc)
Sub DLine(a1 As Double, b1 As Double, a2 As Double, b2 As Double)
  'Create a line on the active Sketch/Drawing View
  'Part.CreateLineVB X1, Y1, Z1, X2, Y2, Z2
  Part.SetAddToDB = True
  Part.CreateLineVB CM(a1), CM(b1), 0, CM(a2), CM(b2), 0
  Part.SetAddToDB = False
End Sub

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
WOW!

That works great...

BUT...
Part.SetAddToDB = True
...
Part.SetAddToDB = False


Needs to be...
Part.SetAddToDB True
...
Part.SetAddToDB False


the definition is:
void ModelDoc2.SetAddToDB ( setting)

Input:
(BOOL) setting
TRUE if you want items added directly to the SolidWorks database, FALSE otherwise.

Thanks DSI, have a star...

-Josh
 
Sorry about that, I was going from memory. Happy to help.

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor