Automatic Drawing view sketch, projection, rectangle

Automatic Drawing view sketch, projection, rectangle

NachoShaw
Advisor Advisor
137 Views
5 Replies
Message 1 of 6

Automatic Drawing view sketch, projection, rectangle

NachoShaw
Advisor
Advisor

Hey there!

 

I dont usually work code the drawing environment so im looking for a bit of help 🙂

 

im bringing in my parts like this

NachoShaw_0-1758120315005.png

 

i need to automatically add a sketch to the view ( i can do this)

draw a rectangle around the outside of the part (i cant do this)

constrain the rectangle at .5in to each side like this

NachoShaw_1-1758120439896.png

 

Can any of you kind coders point me in the direction of success?

 

 

Much appreciated 

 

 

Nacho

 

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


0 Likes
138 Views
5 Replies
Replies (5)
Message 2 of 6

J_Pfeifer_
Advocate
Advocate

Alright, after far too long of messing about with different things I've got an example. One that others may be able to explain the math behind (For my sanity). 

 

The below code will place a Centered rectangle on the view at the exact size of the view. An important note with sketches inside of drawings. When the sketch is a child of the sheet object. The 0, 0 origin location will be the bottom left of the sheet. However, if the sketch is a child of the drawing view, the 0,0 becomes the origin of the sketch. If you try to place a sketch object of any kind, using the view location, but have a drawing view sketch. It will appear off the sheet typically. 

 

Now, What I don't understand is the conversions and math I had to manipulate to get this CenterPoint rectangle to be created properly. MY idea would have just been half the view height. However you will see in the code its more like a 1/6 of the view converted, this became some game of me logging and chasing values to make the rectangle at the proper size. To get the extra .5" on the edges, Its just a bit of addition on the end of the creation. 

 

Sub main()
	
	Dim App As Inventor.Application = ThisApplication
	Dim oDoc As DrawingDocument = App.ActiveDocument
	Dim oTG As TransientGeometry = App.TransientGeometry
	
	Dim oSheet As Sheet = oDoc.Sheets.Item(1) 'only one sheet on doc	
	Dim OnlyView As DrawingView = oSheet.DrawingViews.Item(1) 'only one view on doc. 
	
	Dim ViewSketch As Sketch = OnlyView.Sketches.Add()
	
ViewSketch.Edit() 'You must edit the sketch to work within the view sketch. 
	
	Dim ViewCenterPoint As Point2d = oTG.CreatePoint2d(0, 0) 'The drawing sketch will always place the starting point in the center of the view.
	
	Logger.Info("View width: " & OnlyView.Width & " View height: " & OnlyView.Height)
	
	Dim HalfWidth As Double = OnlyView.Width  / 4
	Dim HalfHeight As Double = OnlyView.Height / 4
	
	Logger.Info("Half Width: " & HalfWidth /2 & " Half height: " & HalfHeight /2)
	
	Dim ViewCornerPoint As SketchPoint = ViewSketch.SketchPoints.Add(oTG.CreatePoint2d(HalfWidth /2,  HalfHeight /2))
	
	ViewSketch.SketchLines.AddAsTwoPointCenteredRectangle(ViewCenterPoint, ViewCornerPoint)
	
ViewSketch.ExitEdit() 'Exit the edit or leave ilogic stuck in sketch. 
	

End Sub

 

This should give you enough insight to factor in what you'd like to do. If for whatever reason you need to create a rectangle based on the size of the item, rather than the view. Things will get more tricky and require others insight. I wrote like 60 lines before I gave up trying to convert drawing curves into doubles. Became more than I was wanting to investigate without direct experience. What I will add, is this is how my drawing template imports standard views. It then runs two views with an invisible rectangle built off the two views to run through a sorting algorithm. Making sure the imported views do not overlap.   

Message 3 of 6

Curtis_Waguespack
Consultant
Consultant

@NachoShaw, here's a quick example, using a top view of a basic block as shown. 

Hope that helps,
Curtis

Edit to add version 2

 

 

Curtis_Waguespack_0-1758149183847.png

 

Dim InventorApp As Inventor.Application = ThisApplication 
Dim oDrawingDoc As DrawingDocument = InventorApp.ActiveDocument 
Dim activeSheet As Sheet 
Dim oView As DrawingView 
oSheet = oDrawingDoc.ActiveSheet 
oView = oSheet.DrawingViews(1) 

Dim oSketch As DrawingSketch 
Dim oCurve As DrawingCurve 
oSketch = oView.Sketches.Add() 
oSketch.Edit 
For Each oCurve In oView.DrawingCurves 
Try 
oSketch.AddByProjectingEntity(oCurve) 
Catch 
End Try 
Next 

Dim oCollection As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection() 

oCollection.Add(oSketch.SketchLines(1)) oCollection.Add(oSketch.SketchLines(2)) oCollection.Add(oSketch.SketchLines(3)) oCollection.Add(oSketch.SketchLines(4)) 

oSketch.OffsetSketchEntitiesUsingDistance(oCollection, 0.1, True, False, true) 

oSketch.DimensionConstraints.AddTwoPointDistance(oSketch.SketchLines(1).EndSketchPoint, oSketch.SketchLines(5).EndSketchPoint, DimensionOrientationEnum.kHorizontalDim, oView.Center)

 oSketch.ExitEdit

 

version 2, drawing rectangle ( rather then offsetting it)

Curtis_Waguespack_0-1758205022518.png



Dim InventorApp As Inventor.Application = ThisApplication
Dim oDrawingDoc As DrawingDocument = InventorApp.ActiveDocument

Dim activeSheet As Sheet
Dim oView As DrawingView

oSheet = oDrawingDoc.ActiveSheet
oView = oSheet.DrawingViews(1)

Dim oSketch As DrawingSketch
Dim oCurve As DrawingCurve


oSketch = oView.Sketches.Add()
oSketch.Edit

For Each oCurve In oView.DrawingCurves
	Try
		oSketch.AddByProjectingEntity(oCurve)
	Catch
	End Try
Next

offset = 0.1 in
x = (oView.Width / 2 / oView.Scale) + offset
y = oView.Height / 2 / oView.Scale + offset
pt1 = ThisApplication.TransientGeometry.CreatePoint2d(-x, -y)
pt2 = ThisApplication.TransientGeometry.CreatePoint2d(x, y)
textPt = ThisApplication.TransientGeometry.CreatePoint2d(x+1, y+1)

oSketch.SketchLines.AddAsTwoPointRectangle(pt1, pt2)

Dim Dimension1 = oSketch.DimensionConstraints.AddTwoPointDistance(oSketch.SketchLines(1).EndSketchPoint,
oSketch.SketchLines(5).EndSketchPoint, DimensionOrientationEnum.kHorizontalDim, textPt)
oSketch.ExitEdit

 

EESignature

Message 4 of 6

J_Pfeifer_
Advocate
Advocate

Is that what I was misunderstanding in the math? Similar to how the sheet view reports the sheet location. When creating a rectangle using the size of the view. The scale of this view needs to be adjusted for? As when you place anything on a drawing sketch, its of a 1 to 1 to the sheet scale? If true, I've got to back track and make some modifications to my other programs. 

 

I was using views that get imported from a set size. Due to this importing of standard sized views, I just created an offset and size using generic math. Now that I know this, this math should just be tied to the view scale specifically?

0 Likes
Message 5 of 6

Curtis_Waguespack
Consultant
Consultant

@J_Pfeifer_ , I might not get this 100% correct, but I from memory when we use View.Sketch we're associating the sketch to the view the sketch coordinates and scale are associated with view's coordinates and scale. 

 

 

 

EESignature

0 Likes
Message 6 of 6

NachoShaw
Advisor
Advisor

@Curtis_Waguespack @J_Pfeifer_ 

Thanks you both for your replies. I was able to to first use Curtis example that was local to the view by using the drawing view size and to be fair it filled what i needed for most part but not 100%, until Curtis added the update which does fill what i need 100% so thanks for that 🙂

 

I appreciate you both for spending the time to help me out. I added in dimensions and some line styles / colors to complete my task

 

 

Thanks fellas

 

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.